Share via


レッスン 10 : サブスクライバとサブスクリプションの追加

このレッスンでは、2 つの VBScript ファイルを検証し、スクリプトがサブスクライバ データやサブスクリプション データをどのように Notification Services に送信するのかを理解します。次に、サブスクライバ データをインスタンス データベースに追加し、サブスクリプション データをアプリケーション データベースに追加するスクリプトを実行します。

サブスクライバとサブスクリプションの管理

Notification Services は、サブスクライバによって作成されたサブスクリプションに基づき、通知を生成します。このサブスクライバは、ユーザーまたはアプリケーションのいずれかになります。Notification Services アプリケーション開発の一環として、作成したサブスクリプションをサブスクライバが管理できるよう、インターフェイスを 1 つ以上作成します。

サブスクライバ データには、電子メール アドレスやテキスト メッセージング アドレスなど、サブスクライバ ID およびサブスクライバが使用するデバイスの情報があります。各サブスクライバは、複数のデバイスを持つことができます。

サブスクライバ データは、インスタンス データベース オブジェクトに格納されます。Notification Services のインスタンスがホストするすべてのアプリケーションは、同じサブスクライバ データを共有します。

サブスクリプション データは、サブスクライバが関心を持つアプリケーション情報 (天気を予想する都市など)、および通知を受け取るデバイスを指定します。アプリケーションには、サブスクライバのデバイスを選択するロジックが内蔵されている場合があります。この場合は、日付、時間、メッセージの重要度などの条件ロジックに基づいてデバイスが選択されます。

サブスクリプション データは、アプリケーション データベースに格納されます。各アプリケーションには、それぞれに固有のサブスクリプション データがあります。

Notification Services アプリケーションのほとんどは、サブスクライバおよびサブスクリプション データを管理するためのインターフェイスを提供しています。Notification Services アプリケーションとしては、ASP.NET アプリケーションがよく使われます。ただし、Notification Services のサブスクリプション管理オブジェクトを使用することで、Windows アプリケーションや Web サービスなどの任意のインターフェイスを開発できます。詳細については、「サブスクリプション管理インターフェイスの開発」を参照してください。

Weather サブスクリプション管理インターフェイスの検証

Weather アプリケーションは、2 つの VBScript ファイルを使用し、サブスクライバおよびサブスクリプション データをまとめて読み込みます。これによって操作が簡略化されます。このような処理は、サブスクライバとサブスクリプションを管理する方法としては一般的ではありません。しかし、作成した通知アプリケーションを試験的に運用し、テストするには便利です。

この 2 つのスクリプトから、Notification Services インターフェイスを COM コンポーネントから呼び出すしくみを理解できます。Notification Services はマネージ コード (C#) で記述されており、マネージ コードおよびアンマネージ コードのどちらからでもアクセスできるように設計されています。

AddSubscribers.vbs

AddSubscribers.vbs と呼ばれる 1 番目の VBScript は、3 つのサブスクライバを Weather インスタンスに追加します。

まず、NSInstance オブジェクトと Subscriber オブジェクトを作成します。

Dim nsInstance, nsSubscriber, nsSubscriberDevice
' Create and initialize NSInstance object.
Set nsInstance = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.NSInstance")
nsInstance.Initialize "Tutorial"
' Create and initialize NSSubscriber object.
Set nsSubscriber = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.Subscriber")
nsSubscriber.Initialize (nsInstance)

次に、Subscriber オブジェクトを使用して 3 つのサブスクライバを追加します。

' Add subscribers.
nsSubscriber.SubscriberId = "stephanie"
nsSubscriber.Add

nsSubscriber.SubscriberId = "david"
nsSubscriber.Add

nsSubscriber.SubscriberId = "richard"
nsSubscriber.Add

最後に、SubscriberDevice オブジェクトを作成し、サブスクライバ デバイスを各サブスクライバに追加します。

' Create NSSubscriberDevice object.
Set nsSubscriberDevice = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.SubscriberDevice")
nsSubscriberDevice.Initialize (nsInstance)

' DeviceName must match subscriptions that use this device
nsSubscriberDevice.DeviceName = "myDevice"

' Add a file device for each subscriber
nsSubscriberDevice.SubscriberId = "stephanie"
nsSubscriberDevice.DeviceTypeName = "File"
nsSubscriberDevice.DeviceAddress = "stephanie@adventure-works.com"
nsSubscriberDevice.DeliveryChannelName = "FileChannel"
nsSubscriberDevice.Add

nsSubscriberDevice.SubscriberId = "david"
nsSubscriberDevice.DeviceTypeName = "File"
nsSubscriberDevice.DeviceAddress = "david@adventure-works.com"
nsSubscriberDevice.DeliveryChannelName = "FileChannel"
nsSubscriberDevice.Add

nsSubscriberDevice.SubscriberId = "richard"
nsSubscriberDevice.DeviceTypeName = "File"
nsSubscriberDevice.DeviceAddress = "richard@adventure-works.com"
nsSubscriberDevice.DeliveryChannelName = "FileChannel"
nsSubscriberDevice.Add

AddSubscriptions.vbs

AddSubscriptions.vbs と呼ばれるもう 1 つの VBScript は、3 つのサブスクライバのそれぞれにサブスクリプションを 1 つ追加します。

まず、NSInstanceNSApplication、および Subscription オブジェクトを作成し、これらを初期化します。

Dim nsInstance, nsApplication, nsSubscription
' Create NSInstance object.
Set nsInstance = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.NSInstance")
nsInstance.Initialize "Tutorial"

' Create NSApplication object.
Set nsApplication = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.NSApplication")
nsApplication.Initialize (nsInstance), "Weather"

' Create Subscription object.
Set nsSubscription = WScript.CreateObject("Microsoft.SqlServer.NotificationServices.Subscription")
nsSubscription.Initialize (nsApplication), "WeatherCity"

次に、読み込もうとしているすべてのサブスクリプションに共通のプロパティを設定します。

nsSubscription.SetFieldValue "DeviceName", "myDevice"
nsSubscription.SetFieldValue "SubscriberLocale", "en-us"

最後に、サブスクリプションを追加します。

nsSubscription.SubscriberId = "stephanie"
nsSubscription.SetFieldValue "City", "Seattle"
nsSubscription.Add

nsSubscription.SubscriberId = "david"
nsSubscription.SetFieldValue "City", "Orlando"
nsSubscription.Add

nsSubscription.SubscriberId = "richard"
nsSubscription.SetFieldValue "City", "Seattle"
nsSubscription.Add

サブスクライバおよびサブスクリプション データの読み込み

スクリプトの内容がわかったので、次は、これらのスクリプトを使用してサブスクライバ データを Tutorial インスタンスに読み込み、サブスクリプション データを Weather アプリケーションに読み込みます。

サブスクライバおよびサブスクリプション データを読み込むには

  1. Windows エクスプローラで、Tutorial の Weather フォルダを開きます。このフォルダの既定の場所は、C:\Program Files\Microsoft SQL Server\90\Samples\Notification Services\tutorial\Weather です。

  2. AddSubscribers.vbs をダブルクリックします。

    "Subscribers successfully added." というメッセージが表示されます。

  3. AddSubscriptions.vbs をダブルクリックします。

    "Subscriptions successfully added." というメッセージが表示されます。

ms167266.note(ja-jp,SQL.90).gifメモ :
SQL Server 2005 Notification Services をインストールした後に Notification Services 2.0 をインストールすると、COM 相互運用には不適切なバージョン構成が登録されるため、これらのスクリプトが動作しません。詳細については、「COM 相互運用のための Notification Services コア アセンブリを登録する方法」を参照してください。

サブスクライバ データの表示

サブスクライバ データをアプリケーションに追加したら、ViewSubscribersAndDevices.sql クエリを使用し、サブスクライバとデバイス情報を表示できます。

サブスクライバ データを表示するには

  1. ソリューション エクスプローラで [Weather] を展開し、[クエリ] を展開します。次に、ViewSubscribersAndDevices.sql をダブルクリックします。

  2. F5 キーを押してクエリを実行します。

    3 つのサブスクライバのサブスクライバ情報およびデバイス情報が表示されます。

  3. ViewSubscribersAndDevices.sql を閉じます。

サブスクリプション データの表示

サブスクリプション データをアプリケーションに追加したら、ViewSubscriptions.sql クエリを使用し、サブスクリプション情報を表示できます。

サブスクリプション データを表示するには

  1. ソリューション エクスプローラで ViewSubscriptions.sql をダブルクリックします。

  2. F5 キーを押してクエリを実行します。

    各サブスクライバについて、3 つのサブスクリプションが表示されます。

  3. ViewSubscriptions.sql を閉じます。

次のレッスン

レッスン 11 : Weather アプリケーションへのイベントの送信

参照

概念

Notification Services のチュートリアル

その他の技術情報

サブスクリプション管理インターフェイスの開発
通知ソリューションの作成
SQL Server Notification Services の紹介

ヘルプおよび情報

SQL Server 2005 の参考資料の入手