Notification Services Tutorial

Embed Size (px)

Citation preview

  • 8/3/2019 Notification Services Tutorial

    1/36

    Notification Services Tutorial

    The Notification Services tutorial teaches you how to build a simple Notification Servicesapplication. The tutorial uses a SQL Server Management Studio solution to make it easyto edit tutorial files and then build the solution using SQL Server Management Studio

    Object Explorer.

    What You Will Learn

    In this tutorial, you will build a weather notification application. This application will letusers subscribe to and receive weather notifications for individual cities.

    You will start with a minimal application definition file (ADF) and use a predefinedinstance configuration file (ICF) to create an instance of Notification Services that hoststhe weather application. The information in the ICF and ADF is compiled and then usedto create an instance database and an application database. The databases are used to

    store data and run the application.

    You will then update the ADF to describe the weather event, notification, andsubscription data, and configure application execution settings. Every time that youupdate the application, you apply the changes to the application database.

    In the last two lessons of this tutorial, you will add subscriber and subscription data, andthen add event data and view the resulting notifications.

    This is a simple application that does not use some of the more advanced features ofNotification Services, such as digest or multicast delivery, custom event providers,

    custom content formatters, and custom delivery protocols. However, this tutorial doesshow the core steps for building a notification application that uses Notification Services.

    Requirements

    Before you start the tutorial, you should be familiar with Notification Services conceptsand terminology. To learn about Notification Services, see Introducing SQL ServerNotification Services.

    This tutorial is intended to be used on a single computer that has all the required softwareinstalled. Make sure that you have done the following:

    You have installed the Database Engine, Notification Services, and WorkstationComponents. For more information, see Installing SQL Server 2005.

    You have installed the Notification Services samples. For more information, seeInstalling Samples.

    You have started an instance of the Database Engine. For more information, seeStarting and Restarting Services.

    http://msdn.microsoft.com/en-us/library/ms166495(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166495(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms143516(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms143516(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms161556(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms190917(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms190917(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166495(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166495(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms143516(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms161556(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms190917(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    2/36

    Note:

    Before reviewing tutorials, you should add Next and Previous buttons to the documentviewer toolbar. For more information, seeAdding Next and Previous Buttons to Help.

    Contents

    This tutorial is divided into 11 lessons. You must complete all tutorial lessons in order todevelop a working Notification Services application.

    Lesson 1: Opening and Examining the Tutorial Solution

    In this lesson, you open the SQL Server Management Studio solution andexamine the files in the solution.

    Lesson 2: Performing an Initial Build of the Tutorial Instance

    In this lesson, you review the application definition file (ADF) for the Weather

    application and the instance configuration file (ICF) for the Tutorial instance ofNotification Services, and then perform an initial build of the instance andapplication.

    Lesson 3: Adding an Event Class

    In this lesson, you add an event class to the ADF that describes event data.

    Lesson 4: Adding a Notification Class

    In this lesson, you add a notification class to the ADF that describes notification

    data, formatting, and delivery.

    Lesson 5: Adding a Subscription Class

    In this lesson, you add a subscription class to the ADF that describes subscriptiondata and the rules that generate notifications.

    Lesson 6: Adding an Event Provider

    In this lesson, you add a non-hosted event provider declaration to the ADF. Thisis used to identify events added using the event submission stored procedures.

    Lesson 7: Configuring the Generator and Distributor

    In this lesson, you specify where the application's generator and distributor run,and the polling interval for the distributor.

    Lesson 8: Altering Application Execution Settings

    http://msdn.microsoft.com/en-us/library/ms167232(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167232(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170650(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171045(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167346(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170473(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166580(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms169899(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166798(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166563(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167232(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170650(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171045(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167346(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170473(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166580(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms169899(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166798(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166563(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    3/36

    In this lesson, you specify generator, distributor logging, and data removalsettings.

    Lesson 9: Registering, Enabling, and Starting the Instance

    In this lesson, you register, enable, and start the instance of Notification Services.

    Lesson 10: Adding Subscribers and Subscriptions

    In this lesson, you add subscribers to the instance database and subscriptions tothe application database.

    Lesson 11: Submitting Events to the Weather Application

    In this lesson, you add events to the application and then view the resultingnotifications.

    Lesson 1: Opening and Examining the Tutorial Solution

    In this lesson you will open the SQL Server Management Studio solution for the tutorialand review the projects and files. You will also review Object Explorer as it pertains tothis tutorial. After you complete this lesson, you should be familiar with the solution andready to start working with the solution files.

    Opening the Tutorial Solution

    First, let's open the tutorial solution you will be working with.

    To open the tutorial solution

    1. In Windows Explorer, locate the Notification Services Tutorial folder. The defaultlocation for this folder is C:\Program Files\Microsoft SQLServer\90\Samples\Notification Services\tutorial.

    2. Open the NSTutorial.ssmssln file.

    SQL Server Management Studio opens with solution files displayed in SolutionExplorer. If Solution Explorer and Object Explorer do not appear, select them onthe View menu.

    3. Connect to the instance of the Database Engine where you want to store data forthe instance of Notification Services.

    1. In Object Explorer, clickConnect and then select Database Engine.The Connect to Server dialog box appears.

    2. In the Server name text box, select or enter the name of the DatabaseEngine instance where you want to store Notification Services data for thistutorial.

    http://msdn.microsoft.com/en-us/library/ms170375(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167266(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170427(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170375(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms167266(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms170427(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    4/36

    3. In the Authentication text box, select an authentication mode.If you select SQL Server Authentication, you must enter a user name andpassword.

    4. Click Connect.

    Examining the Tutorial Solution

    In Solution Explorer, you should see two projects: Tutorial and Weather. The Tutorialproject contains files for the Tutorial instance of Notification Services. The Weatherproject contains files for the Weather application. The Tutorial instance hosts theWeather application.

    Tutorial Project

    First, expand the Tutorial project and expand the Miscellaneous folder. The Tutorialproject contains only one file, TutorialICF.xml. This file describes the Tutorial instance

    of Notification Services.

    Weather Project

    Next, examine the Weather project. This project contains files used to update and run theWeather application and to review database changes after performing some tutoriallessons.

    The Queries folder contains Transact-SQL queries that you will later use toreview database changes and submit events to the Weather application.

    The Miscellaneous folder contains four files:

    o WeatherADF.xml is an application definition file (ADF). This filecontains XML code that describes the Weather application. Most of thetutorial involves editing this file.

    o WeatherTransform.xslt is an Extensible Stylesheet Language

    Transformation (XSLT). The Weather application's XSLT contentformatter uses the code in this file to transform raw notification data intoformatted notifications.

    o AddSubscribers.vbs is a VBScript file used to add subscribers to the

    instance of Notification Services. Typically, you would build asubscription management interface using managed code, but this script is aquick way to load subscriber and subscription data for prototyping.

    o AddSubscriptions.vbs is a VBScript file used to add subscriptions to theWeather application. Again, script files like this are used primarily forprototyping.

    Object Explorer

    In Object Explorer, you will be using the Notification Services and Databases folders.The Notification Services folder is where you will be accessing Notification Services

  • 8/3/2019 Notification Services Tutorial

    5/36

    dialogs to create, update, register, enable, and start the instance of Notification Services.The Databases folder will contain the TutorialNSMain instance and TutorialWeatherapplication database.

    Lesson 2: Performing an Initial Build of the Tutorial InstanceUpdated: 17 July 2006

    In this lesson you will review the instance configuration file (ICF), update the ICF asneeded, review the application definition file (ADF), and then create the Tutorial instanceof Notification Services.

    Reviewing the ICF

    First, review the instance configuration in TutorialICF.xml. The instance configurationdefines an instance of Notification Services, called Tutorial, that hosts the Weather

    application.

    During this tutorial, you will not be modifying TutorialICF.xml, but you should knowwhat the file contains.

    To review the ICF

    1. In Solution Explorer, expand Tutorial, and then expand Miscellaneous.2. Double-clickTutorialICF.xml.

    The ICF contains the following elements:

    o A ParameterDefaults element that contains default values for ICF and

    ADF parameters.o An InstanceName element that specifies the Notification Services

    instance name, which is Tutorial.o A SqlServerSystem element that specifies the instance of SQL Server that

    will host the Notification Services databases. The element contains aparameter, %_DBEngineInstance_%. The value for this parameter isprovided by the parameter defaults, and can be overridden when youcreate or update the instance.

    o An Applications element that contains basic information about the

    Weather application. This includes the application name, folder, ADFlocation, and the names and values of any replaceable parameters in theADF. The Weather application uses two parameters: %_InstancePath_%and %_ServerName_%. Values for these parameters are provided by theparameter defaults, and can be overridden when you create or update theinstance.

    o A DeliveryChannels element that defines a delivery channel for the built-

    in File delivery protocol. The Weather application will use the File

  • 8/3/2019 Notification Services Tutorial

    6/36

    delivery protocol to deliver notifications to the delivery channel namedFileChannel. These notifications will be written to a file namedFileNotifications.htm.

    Editing the ICF ParameterDefaults

    To make updating the tutorial easier, verify that the following values in theParameterDefaults section of the ICF are correct:

    The value for the_DBEngineInstance_parameter must be the name of theDatabase Engine instance you are connected to in Object Explorer. The defaultvalue for this parameter is %COMPUTERNAME%, which is an environmentvariable that gets the name of the local computer. If you are connected to a namedinstance, replace %COMPUTERNAME% with the SQL Server instance name.For the default instance, the value should be the same as the computer name.

    The value for the_ServerName_parameter should be the name of the local

    computer. This value is passed to the ADF, and specifies where the NotificationServices engine components will run. Unless you are scaling-out the instance,which is not recommended for this tutorial, you should leave the value as%COMPUTERNAME%.

    The value for_InstancePath_is the path to the TutorialICF.xml file. If you didnot install the samples to the default location, update this value with the correctpath of the tutorial files. %ProgramFiles% is an environment variable that getsthe path of the Program Files folder on the local computer, which is typicallyC:\Program Files.

    If you have changed any values in the ParameterDefaults section, save and close

    TutorialICF.xml.

    Reviewing the ADF

    Next, review the initial application definition in WeatherADF.xml. You will bemodifying this file during the tutorial.

    To review the ADF

    1. In Solution Explorer, expand Weather and then expand Miscellaneous.2. Double-clickWeatherADF.xml.

    The initial ADF is almost empty. It contains only those elements required for theapplication to build successfully. This includes an empty SubscriptionClasseselement, an empty NotificationClasses element, and the SystemName elementsfor the application's generator and distributor. These elements contain aparameter, %_NSServer_%.The value for this parameter is provided when youcreate the instance of Notification Services and is passed from the ICF to theADF.

  • 8/3/2019 Notification Services Tutorial

    7/36

    Create the Instance of Notification Services

    When you create an instance of Notification Services, you provide Notification Serviceswith the name of the ICF and values for any parameters needed by the ICF. NotificationServices uses the ICF to create an instance database and uses the Weather application's

    ADF that is referenced in the ICF to create an application database.

    You will use SQL Server Management Studio to create the instance of NotificationServices.

    To create the instance of Notification Services

    1. In Object Explorer, right-clickNotification Services and select New NotificationServices Instance.

    2. Click Browse, locate the TutorialICF.xml file, and then clickOpen.

    The default location for this file is C:\Program Files\Microsoft SQLServer\90\Samples\Notification Services\tutorial\TutorialICF.xml.

    3. In the Parameters grid, review the values for the parameters. These should be thevalues you provided if you edited the ICF.

    If you are using the %COMPUTERNAME% and %ProgramFiles% environmentvariables, Notification Services shows the values for these variables, too.

    4. Click OK.

    A dialog shows the status of the create actions. All actions should completesuccessfully. If the actions do not complete successfully, verify that the parametervalues you provided are correct, and then try to create the instance of NotificationServices again.

    5. When the create actions complete, clickClose.

    Review the Results

    After you create the Tutorial instance, you should see two new databases and an instanceof Notification Services in Object Explorer.

    To review the results

    1. In Object Explorer, expand Databases.

    You should see two databases: TutorialNSMain, which is the instance database,and TutorialWeather, which is the application database. If you do not see thesedatabases, right-clickDatabases and select Refresh.

  • 8/3/2019 Notification Services Tutorial

    8/36

    2. Expand Notification Services.

    You should see an instance of Notification Services named Tutorial. If you do notsee the Tutorial instance, right-clickNotification Services and select Refresh.

    Lesson 3: Adding an Event Class

    In this lesson you will add an event class to the application definition file (ADF) and thenupdate the instance of Notification Services to apply the changes to the applicationdatabase. You can then review the changes made to the application database.

    Event Class Primer

    An event class represents one type of event used by your Notification Servicesapplication. When you define an event class, you define a type of event that can besubmitted to your application and used to generate notifications. The event class

    definition includes the event field names, data types, and type modifiers.

    An event class can define other information, such as indexes on the event data andsupplemental tables. You can learn more about event classes by reading Defining EventClasses.

    When you create the application, Notification Services uses the event class definition tocreate tables, views, indexes, and stored procedures for storing, collecting, and managingevent data.

    The WeatherData Event Class

    For this application, you will add a WeatherData event class to the ADF. This eventclass has five event fields, City, Date, Low, High, and Forecast. These describe theevent data you will later submit to the application. For each event field, the event classdefines the field name, data type, and field modifiers.

    To improve performance for notification generation, the event class also specifies anindex on the City field of the WeatherData event class.

    Add the Event Class XML to the ADF

    The XML in this section defines the event class. Examine the XML and then follow theinstructions to copy the XML to the ADF.

    To add the event class XML to the ADF

    1. Click Copy Code to copy the XML to the Windows Clipboard.

    XML

    http://msdn.microsoft.com/en-us/library/ms171294(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171294(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171294(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171294(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    9/36

    Copy

    WeatherData

    City nvarchar(35) not null Date datetime not null Low float not null High float not null Forecast nvarchar(3500) not null

    CREATE INDEX myIndex

    ON WeatherData ( City );

    2. In Solution Explorer, open WeatherADF.xml.3. Replace the following comment with the XML you just copied.

    4. On the File menu, select Save WeatherADF.xml.

    Update the Instance of Notification Services

    After you modify the ADF, you update the instance of Notification Services to add eventclass objects to the application database.

  • 8/3/2019 Notification Services Tutorial

    10/36

  • 8/3/2019 Notification Services Tutorial

    11/36

  • 8/3/2019 Notification Services Tutorial

    12/36

  • 8/3/2019 Notification Services Tutorial

    13/36

    WeatherTransform.xslt File

    2. In Solution Explorer, open WeatherADF.xml.3. Replace the following XML with the XML you just copied.

    4. On the File menu, select Save WeatherADF.xml.

    Update the Instance of Notification Services

    After you modify the ADF, you update the instance of Notification Services to addnotification class objects to the application database:

    To update the instance of Notification Services

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and then select Update.3. In the Update Instance dialog box, clickBrowse, locate the TutorialICF.xml file,

    and then clickOpen.4. In the Parameters grid, review the values for the parameters. Use the same

    values you used to create the instance.5. Click OK.6. Review the Notification Services - Update Summary dialog box, and then click

    Update.7. When the update actions are complete, clickClose.

    Review the Database Changes

    When you update the instance, Notification Services adds tables, views, and storedprocedures for the notification class in the TutorialWeather application database.

    The dbo.WeatherAlerts view is where the notification generation rules willinsert notifications.

  • 8/3/2019 Notification Services Tutorial

    14/36

    The dbo.NSWeatherAlertsFileNotifications view contains notifications fromthe WeatherAlerts notification class sent using the File delivery protocol. Thisview is for internal use only.

    The dbo.NSWeatherAlertsNotificationDistribution view combines data fromseveral internal Notification Services tables to provide information about

    notification distribution attempts. The dbo.NSWeatherAlertsNotifications table is the underlying table that

    contains notification data for the notification class. Notification data stays in thistable until it is removed by the vacuumer data removal process. This table is forinternal use only.

    Caution:

    Do not directly modify data in the notification tables and views.

    You can use Object Explorer to examine these objects in the TutorialWeather database.Tables and views for internal use only contain data that is used for internal operations;

    you do not have to understand this data.

    You may have to refresh Object Explorer to see the new objects.

    Lesson 5: Adding a Subscription Class

    In this lesson, you will add a subscription class to the application definition file (ADF)and then update the instance of Notification Services to apply the changes to theapplication database. You can then review the changes made to the application database.

    Subscription Class Primer

    When you define a subscription class, you are defining storage for subscription data andqueries that generate notifications from event and subscription data.

    When defining storage for subscription data, you define fields for the subscription datayou collect. For example, if part of the subscription data is a city of interest, you can adda City subscription field. Then, you provide an interface to let subscribers createsubscriptions in which they specify a city.

    If your application supports multiple subscriber locales and multiple devices, such as e-mail and cellular phones, you also can add DeviceName and SubscriberLocale fields to

    the subscription fields. These fields let you customize notifications for different devices,time zones, and languages.

    Notification Services adds a SubscriberId field to the underlying table for eachsubscription class.

    Besides defining subscription fields, you must also define a notification generation rule.This rule is a Transact-SQL query that matches event and subscription data and inserts

  • 8/3/2019 Notification Services Tutorial

    15/36

    the results into the notification class view. These rules can be event driven or scheduled.Event-driven rules run whenever events arrive. Scheduled rules evaluate individualsubscriptions on a schedule provided in the subscription.

    A subscription class can define other information, such as indexes on the subscription

    data and supplemental tables. For more information about subscription classes, seeDefining Subscription Classes.

    When you create the application, Notification Services uses the subscription classdefinition to create tables, views, indexes, and stored procedures for the subscriptionclass.

    The WeatherCity Subscription Class

    For this application, you will add a WeatherCity subscription class to the ADF. Thissubscription class has three fields, DeviceName, SubscriberLocale, and City. The City

    field is the important field for this application. When subscribers create weathersubscriptions, they must specify the name of a city.

    The DeviceName and SubscriberLocale subscription fields are defined so that you canadd support for multiple devices and locales.

    This subscription class also defines a notification generation query. To write this query,you must know what event data is available, what notification data you want to produce,and what subscription data is available:

    As shown in Lesson 3, the WeatherData event class has five fields: City, Date,

    Low, High, and Forecast. The subscription class has a City field. As shown in Lesson 4, the WeatherAlerts notification class has the same fields

    as the event class (City, Date, Low, High, and Forecast). Also, remember that the underlying notification table has the following fields:

    SubscriberId, DeviceName, and SubscriberLocale.

    These fields become columns in views. Knowing these fields, you can join the event andsubscription views and insert data into the notification view:

    XML

    CopyINSERT INTO WeatherAlerts(SubscriberId,DeviceName, SubscriberLocale, City, Date, Low, High,Forecast)

    SELECT s.SubscriberId, s.DeviceName, s.SubscriberLocale,e.City, e.Date, e.Low, e.High, e.Forecast

    FROM WeatherData e, WeatherCity sWHERE e.City = s.City;

    http://msdn.microsoft.com/en-us/library/ms171384(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171384(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171384(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    16/36

    When Notification Services runs this query, it joins the current set of events in the eventclass view with the subscriptions in the subscription class view based on matchesbetween the event and subscription City values. Notification Services then inserts theresults into the notification view.

    Add the Subscription Class to the ADF

    The XML in this section defines the subscription class. Examine the XML and thenfollow the instructions to copy the XML to the ADF.

    To add the subscription class to the ADF

    1. Click Copy Code to copy the XML to the Windows Clipboard.

    XML

    Copy

    WeatherCity DeviceName nvarchar(255) not null

    SubscriberLocale nvarchar(10) not null City nvarchar(40) not null WeatherEventRule WeatherData

    INSERT INTO WeatherAlerts(SubscriberId,

    DeviceName, SubscriberLocale, City, Date, Low, High,Forecast)

    SELECT s.SubscriberId, s.DeviceName, s.SubscriberLocale,e.City, e.Date, e.Low, e.High, e.Forecast

    FROM WeatherData e, WeatherCity sWHERE e.City = s.City;

  • 8/3/2019 Notification Services Tutorial

    17/36

    2. In Solution Explorer, open WeatherADF.xml.3. Replace the following XML with the code you just copied.

    4. On the File menu, select Save WeatherADF.xml.

    Update the Instance of Notification Services

    After you modify the ADF, you update the instance of Notification Services to addsubscription class objects to the application database.

    To update the instance of Notification Services

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and then select Update.3. In the Update Instance dialog box, clickBrowse, locate the TutorialICF.xml file,

    and then clickOpen.4. In the Parameters pane, review the parameter values.5. Click OK.

    6. Review the Notification Services - Update Summary dialog box, and then clickUpdate.7. When the update actions complete, clickClose.

    Review the Changes in the Application Database

    When you update the instance, Notification Services adds tables, views, and storedprocedures for the subscription class in the TutorialWeather application database:

    The dbo.WeatherCityView view provides a view of the subscription records forthe associated subscription class.

    The dbo.WeatherCity view contains the current set of subscriptions beingevaluated by the application. In your notification generation query, you selectsubscription data from this view.

    The dbo.NSWeatherCitySubscriptions table is the underlying table thatcontains subscription data for the subscription class.

    Caution:

    Do not directly modify the data in the subscription tables and views. Use the

  • 8/3/2019 Notification Services Tutorial

    18/36

    subscription management objects. For more information, see DevelopingSubscription Management Interfaces.

    You can use Object Explorer to examine these objects in the TutorialWeather database.Tables and views for internal use only contain data used for internal tracking and data

    lookup.

    You may have to refresh Object Explorer to see the new objects.

    Lesson 6: Adding an Event Provider

    In this lesson, you will configure a non-hosted event provider that you will later use whenyou submit events to the Weather application. You will then update the instance ofNotification Services to apply the changes to the application database.

    Event Provider Primer

    Event providers collect event data and submit it to Notification Services. When an eventprovider submits a set of events, it submits the events to an event class view. The set ofevents, called an event batch, is inserted into the event class view. Each event thenbecomes a row in the underlying event class table.

    To help you develop and deploy a Notification Services application quickly, NotificationServices includes three standard hosted event providers, a file system watcher, the SQLServer event provider, and the Analysis Services event provider. Using these eventproviders, you can easily gather events from an XML file, gather events using a Transact-SQL query, and gather events using an MDX query. The Notification Services engine

    hosts all these event providers. This means that the Notification Services engine startsand runs hosted event providers.

    You can use the standard event providers in your application by specifying predefinednames and custom operational parameters in the application definition. For moreinformation, see Defining Event Providers.

    If the standard event providers do not meet the needs of your application, you candevelop event providers that are customized to your environment and event sources. TheNotification Services engine can host your custom event providers, or they can be non-hosted.

    Non-hosted event providers are stand-alone applications that submit events to anotification application without being hosted by the Notification Services engine. Whenyou define a non-hosted event provider, you give the non-hosted event provider a namein the ADF. This name is used for tracking and troubleshooting event collection.

    The WeatherSPEventProvider Event Provider

    http://msdn.microsoft.com/en-us/library/ms166433(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166433(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171379(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166433(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166433(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171379(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    19/36

    For this application, you will add a non-hosted event provider to the ADF. As mentionedearlier, you will just provide a name for this event provider so that you can laterdetermine which events it submitted to the application.

    The name of the event provider will be WeatherSPEventProvider. You will later use

    stored procedures to submit events to the Weather application, and you will use the nameWeatherSPEventProvider when submitting those events.

    Add the Provider Code to the ADF

    The XML in this section defines the non-hosted event provider. Examine the XML andthen follow the instructions to copy the XML to the ADF.

    To add the provider code to the ADF

    1. Click Copy Code to copy the XML to the Windows Clipboard.

    XML

    Copy

    WeatherSPEventProvider

    2. In Solution Explorer, open WeatherADF.xml.3. Replace the following comment with the XML you just copied.

    4. On the File menu, select Save WeatherADF.xml.

    Update the Instance of Notification Services

    After you modify the ADF, you update the instance of Notification Services to add the

    event provider definition to the application database.

    To update the instance of Notification Services

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and then select Update.3. In the Update Instance dialog box, clickBrowse, locate the TutorialICF.xml file,

    and then clickOpen.

  • 8/3/2019 Notification Services Tutorial

    20/36

    4. In the Parameters grid, review the values for the parameters. Use the samevalues you used to create the instance.

    5. Click OK.6. Review the Notification Services - Update Summary dialog box, and then click

    Update.

    7. When the update actions complete, clickClose.

    Review the Changes in the Application Database

    When you update the instance, Notification Services adds information about the eventprovider to the NSProviders table in the TutorialWeather application database.

    To view TutorialWeather event providers

    1. In Solution Explorer, expand Weather, and then expand Queries.2. Double-clickViewEventProviders.sql.

    You may have to connect to the instance of the Database Engine.

    3. Press F5 to run the Transact-SQL query.

    Lesson 7: Configuring the Generator and DistributorUpdated: 5 December 2005

    In this lesson, you will review generator and distributor settings in the applicationdefinition file (ADF) and then modify and update the distributor settings.

    Generator and Distributor Primer

    The generator that is run by the Notification Services engine governs the rule processingfor a notification application. Settings for the generator determine which computer runsthe generator and how many threads the generator can use when it processes applicationrules.

    Each application has one generator. For more information about generators, seeSpecifying Generator Settings.

    Distributors, which are also run by the Notification Services engine, govern notification

    formatting and delivery. On a configurable interval, called a distributor quantum, thedistributor looks for a group of notifications, called a work item, to process. If a workitem is available, the distributor acquires the work item, calls the content formatter totransform the notification data, and then sends the formatted notifications using adelivery protocol.

    http://msdn.microsoft.com/en-us/library/ms172466(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172466(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    21/36

  • 8/3/2019 Notification Services Tutorial

    22/36

    Update the Instance of Notification Services

    After you modify the ADF, you update the instance of Notification Services to modifydistributor settings in the application database.

    To update the instance of Notification Services

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and then clickUpdate.3. In the Update Instance dialog box, clickBrowse, locate the TutorialICF.xml file,

    and then clickOpen.4. In the Parameters pane, review the values for the parameters. Use the same

    values you used to create the instance.5. Click OK.6. Review the Notification Services - Update Summary dialog box, and then click

    Update.

    7. When the update is completed, clickClose.

    Lesson 8: Altering Application Execution Settings

    In this lesson you will configure application execution settings in the applicationdefinition file (ADF) and then update the instance of Notification Services to apply thechanges to the application database.

    Application Execution Settings

    Notification Services provides several application execution settings for tuning your

    application. These settings control how frequently data is processed, how far thegenerator is allowed to fall behind the real-time clock, how events are processed, howmuch data can be received and sent, how distribution data is logged, and how frequentlyold data is removed from the database.

    For more information about these settings, see Specifying Application ExecutionSettings.

    Application Execution Settings for the Weather Application

    For this application, you will configure three application execution settings: the generator

    quantum, distributor logging, and the data removal interval. You should configure thesesettings in every application.

    You will set the quantum duration to 15 seconds. This is PT15S in the XMLduration format. This configures the generator to fire subscription rules every 15seconds. In your applications, this quantum duration might be longer. For moreinformation, see Specifying the Generator Quantum Duration.

    http://msdn.microsoft.com/en-us/library/ms171204(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171204(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171315(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171204(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171204(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171315(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    23/36

    You will turn off distributor logging to minimize the data logged to theapplication database per notification. You may want to enable distributor loggingduring application development, but you should turn off all or most distributorlogging when you deploy your applications. For more information, seeConfiguring Distributor Logging.

    You will specify a data removal interval so that older event, notification, anddistribution data is regularly removed from the application database. Thevacuuming schedule specifies that data older than one day can be deleted from thedatabase, and that the data removal process runs at 23:00:00 UTC. For moreinformation, see Configuring Data Removal.

    Add the Application Execution Settings Code to the ADF

    The XML in this section configures application execution settings as described earlier.Examine the XML and then follow the instructions to copy the XML to the ADF.

    To add the application execution settings code to the ADF

    1. Click Copy Code to copy the code to the Windows Clipboard.

    XML

    Copy

    PT15S

    false false false P1D 23:00:00 P0DT02H00M00S

    2. In Solution Explorer, open WeatherADF.xml.3. Replace the following comment with the code you just copied.

    4. On the File menu, clickSave WeatherADF.xml.

    http://msdn.microsoft.com/en-us/library/ms166490(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171064(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166490(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171064(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    24/36

    Update the Instance of Notification Services

    After you modify the ADF, update the instance of Notification Services to modify theapplication execution settings in the application database.

    To update the instance of Notification Services

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and then select Update.3. In the Update Instance dialog box, clickBrowse, locate the TutorialICF.xml file,

    and then clickOpen.4. In the Parameters pane, enter values for the parameters. Use the same values you

    used to create the instance.5. Click OK.6. Review the Notification Services - Update Summary dialog box, and then click

    Update.

    7. When the update is completed, clickClose.8. You can now close WeatherADF.xml.

    Lesson 9: Registering, Enabling, and Starting the InstanceUpdated: 17 July 2006

    In this lesson, you will configure security for the instance of Notification Services andregister the instance of Notification Services that installs the Notification Services engine.Then, you will enable and start the instance.

    The Notification Services Engine

    Earlier in this tutorial, you configured a generator and a distributor for the application.These components are run by the Notification Services engine. If the application had useda hosted event provider, that event provider also would be run by the NotificationServices engine.

    The Notification Services engine is either a Windows service that you create when youregister the instance on a computer, or a process hosted by a custom application.

    When you configure hosted event providers, generators, and distributors in theapplication definition file (ADF), you specify a system name, which is the computer

    where that component runs. You must install and run the Notification Services engine oneach computer specified by the system name values in the ADF.

    When you start the Notification Services engine, it connects to the instance andapplication databases to determine what components it runs and if those components areenabled. The engine then runs its enabled components.

  • 8/3/2019 Notification Services Tutorial

    25/36

    For more information about the Notification Services engine, see Controlling Instanceand Application Operation.

    The Notification Services engine connects to the instance and application databases andruns its stored procedures. To run these stored procedures, the engine must have login

    access to the server and the correct database permissions. These permissions are grantedto the login through membership in database roles in the instance and applicationdatabases. For more information, see Configuring SQL Server Permissions for anInstance of Notification Services.

    The Engine for the Weather Application

    For this tutorial, you will run the Notification Services engine as a Windows service. Youwill install the Windows service, named NS$Tutorial, when you register the instance ofNotification Services. You will register the instance locally. This is the same computername you provide for the_NSServer_parameter when you create or update the instance

    of Notification Services.

    When you register the instance, you must provide a Windows user name and password.The Windows service can use its Windows account or a SQL Server login you specify tolog in to the database server. You must add a user for the Windows account or SQLServer login to the TutorialNSMain and TutorialWeather databases and then add theuser to database roles. You use this same account when you register the instance ofNotification Services. For more information about the security model, see SecurityConsiderations for Notification Services.

    After you register the instance and configure security, you will enable and start the

    instance of Notification Services.

    Configure Database Permissions for the Windows Service

    Use the following steps to configure database permissions for the account used by theWindows service to connect to the TutorialNSMain and TutorialWeather databases.

    To configure database permissions for the Windows service

    1. Choose a Windows account for the NS$Tutorial Windows service.

    When you deploy an instance of Notification Services, use an account that hasfew privileges, and then grant only the necessary permissions to that account.

    2. If you can use Windows Authentication, make sure this Windows account can login to the database server. If you cannot use Windows Authentication, make surethere is a SQL Server login that the Windows service can use.

    http://msdn.microsoft.com/en-us/library/ms172486(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172486(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172592(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172592(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172604(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172604(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172486(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172486(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172592(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172592(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172604(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms172604(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    26/36

    To add a new login in Object Explorer, expand Security, right-clickLogins, andselect New Login. You can add Windows accounts or create SQL Server loginaccounts using this dialog box.

    3. Using Object Explorer, create a database user for the login in the

    TutorialNSMain database and make the user a member of the NSRunServicedatabase role:1. Expand Databases, and then expand the TutorialNSMain database.2. Right-clickSecurity, point to New, and then select User.3. In the User name box, enter a name for the database user.

    The database user name is typically the same as the login name.4. In the Login name box, enter the name of the login that is used by the

    NS$Tutorial Windows service.The login name must exactly match the name of an existing login in theinstance of the Database Engine. Click the browse (...) button to search forthe login name.

    5. In the Database role membership box, clickNSRunService.6. Click OK.4. Create a database user for the login in the TutorialWeather database, and make

    the user a member of the NSRunService database role:1. Expand the TutorialWeather database.2. Right-clickSecurity, point to New, and then select User.3. In the User name box, enter a name for the database user.

    The database user name is typically the same as the login name.4. In the Login name box, enter the name of the login that is used by the

    NS$Tutorial Windows service.The login name must exactly match the name of an existing login in the

    instance of the Database Engine. Click the browse ( ... ) button to searchfor the login name.5. In the Database role membership box, clickNSRunService.6. Click OK.

    Configure Folder Permissions for the Windows Service

    Because the Weather application uses the File delivery protocol to deliver notifications,you must also make sure that the Windows service account has permissions in the folderwhere it writes notifications.

    To configure folder permissions for the Windows service

    1. Open Windows Explorer.2. Locate the Notification Services Tutorial folder.

    The default location for this folder is C:\Program Files\Microsoft SQLServer\90\Samples\Notification Services\tutorial.

  • 8/3/2019 Notification Services Tutorial

    27/36

    3. Expand the Tutorial folder, and then expand the Weather folder.4. Right-click the Notifications folder and select Sharing and Security.5. On the Security tab, verify that the Windows account used by the NS$Tutorial

    Windows service has write permissions.

    Register the Instance and Create a Windows Service

    Use the following steps to register the instance of Notification Services locally and installthe NS$Tutorial Windows service. During this step, Notification Services grantsWindows permissions to the service account.

    Register the instance and create a Windows service

    1. In Object Explorer, expand Notification Services.2. Right-clickTutorial, point to Tasks, and select Register.3. Select the Create Windows service check box.

    4. Enter the login account and password for the NS$Tutorial Windows service.5. If you use SQL Server Authentication to access databases, select SQL Server

    Authentication and enter the SQL Server login and password. Otherwise, leaveWindows Authentication selected.

    6. Click OKto register the instance, create the Windows service, and createperformance counters.

    7. When the registration is complete, clickClose.

    Enable and Start the Tutorial Instance

    Use the following steps to enable the Tutorial instance and start the NS$Tutorial

    Windows service.

    To enable and start the tutorial instance

    1. In Object Explorer, right-clickTutorial and select Enable.2. Right-clickTutorial, and select Start.

    If the service does not start, either the service does not have a valid Windows account, itdoes not have the necessary permissions, or it cannot connect to the TutorialNSMain orTutorialWeather database. Verify that the account that is used by the service has thepermissions that were discussed earlier. For more information, seeSecuring Notification

    Services.

    Checking Tutorial Instance Status

    To verify that the application is configured correctly and that the Tutorial instance isenabled and started, view the status of the Tutorial instance.

    To check tutorial instance status

    http://msdn.microsoft.com/en-us/library/ms166500(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166500(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166500(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166500(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms166500(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    28/36

    1. Right-click the instance and select Properties.

    On the Applications page, you can view the status of all application components.In the Current Status column, all components should show Enabled. If the statusis Enable Pending, wait 30 seconds and then clickRefresh.

    If the status is still Enable Pending, the system name of the component does notmatch the name of the computer where the Notification Services engine isrunning. Verify that the Server values on the Application page and the WindowsServices page match.

    On the Windows Servicespage, you can verify that the NS$Tutorial Windowsservice is running and, in the Associated components box, you can view whichcomponents are enabled on the Windows service. You should see one generatorand one distributor.

    Next Lesson

    Even though the Tutorial instance is enabled and running, it will not produce notificationsuntil you have added subscribers, subscriptions, and events. In the next lesson, you willadd subscribers and subscriptions.

    Lesson 10: Adding Subscribers and Subscriptions

    In this lesson, you will review two VBScript files to understand how they submitsubscriber and subscription data to Notification Services, and then you will run the scriptsto add subscriber data to the instance database and subscription data to the application

    database.

    Subscriber and Subscription Management

    Notification Services generates notifications based on subscriptions created bysubscribers. These subscribers can be people or other applications. As part of developinga Notification Services application, you develop one or more interfaces so thatsubscribers can manage their subscriptions.

    Subscriber data includes a subscriber ID and information about the subscriber's devices,such as e-mail addresses and text messaging addresses. Each subscriber can have

    multiple devices.

    Subscriber data is stored in instance database objects. All applications hosted by aninstance of Notification Services use the same subscriber data.

    Subscription data specifies what application information the subscriber is interested in,such as the city for weather forecasts, and which device receives the notifications. Your

  • 8/3/2019 Notification Services Tutorial

    29/36

  • 8/3/2019 Notification Services Tutorial

    30/36

    nsSubscriber.SubscriberId = "david"nsSubscriber.Add

    nsSubscriber.SubscriberId = "richard"nsSubscriber.Add

    Finally, create a SubscriberDeviceobject and add subscriber devices for each subscriber:

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

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

    ' Add a file device for each subscriber

    nsSubscriberDevice.SubscriberId = "stephanie"nsSubscriberDevice.DeviceTypeName = "File"nsSubscriberDevice.DeviceAddress = "[email protected]"nsSubscriberDevice.DeliveryChannelName = "FileChannel"nsSubscriberDevice.Add

    nsSubscriberDevice.SubscriberId = "david"nsSubscriberDevice.DeviceTypeName = "File"nsSubscriberDevice.DeviceAddress = "[email protected]"nsSubscriberDevice.DeliveryChannelName = "FileChannel"nsSubscriberDevice.Add

    nsSubscriberDevice.SubscriberId = "richard"nsSubscriberDevice.DeviceTypeName = "File"nsSubscriberDevice.DeviceAddress = "[email protected]"nsSubscriberDevice.DeliveryChannelName = "FileChannel"nsSubscriberDevice.Add

    AddSubscriptions.vbs

    The other VBScript, called AddSubscriptions.vbs, adds one subscription for each of ourthree subscribers.

    First, create and initialize the NSInstance,NSApplication, and Subscriptionobjects:

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

    ' Create NSApplication object.

    http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscriberdevice(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscriberdevice(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.nsapplication(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.nsapplication(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscription(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscription(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscriberdevice(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.nsapplication(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/microsoft.sqlserver.notificationservices.subscription(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    31/36

    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"

    Next, set the common properties for all subscriptions you are loading:

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

    Finally, add the subscriptions:

    CopynsSubscription.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

    Load Subscriber and Subscription Data

    Now that you are familiar with the scripts, use them to load subscriber data into theTutorial instance and subscription data into the Weather application.

    To load subscriber and subscription data

    1. Using Windows Explorer, locate the tutorial Weather folder. The default locationfor this folder is C:\Program Files\Microsoft SQL Server\90\Samples\NotificationServices\tutorial\Weather.

    2. Double-click AddSubscribers.vbs.

    You should receive the message, "Subscribers successfully added."

    3. Double-click AddSubscriptions.vbs.

    You should receive the message, "Subscriptions successfully added."

    Note:

  • 8/3/2019 Notification Services Tutorial

    32/36

    If you install Notification Services 2.0 after you install SQL Server 2005 NotificationServices, the wrong versions of the assemblies will be registered for COM interop andthese scripts will not work. For more information, seeHow to: Register the CoreNotification Services Assembly for COM Interop.

    View Subscriber Data

    After you add subscriber data to the application, you can view the subscriber and deviceinformation using the ViewSubscribersAndDevices.sql query.

    To view subscriber data

    1. In Solution Explorer, expand Weather, expand Queries, and then double-clickViewSubscribersAndDevices.sql .

    2. Press F5 to run the query.

    You should see subscriber and device information for three subscribers.

    3. Close ViewSubscribersAndDevices.sql.

    View Subscription Data

    After you add subscription data to the application, you can view the subscriptionsinformation using the ViewSubscriptions.sql query.

    To view subscription data

    1. In Solution Explorer, double-clickViewSubscriptions.sql.

    2. Press F5 to run the query.

    You should see three subscriptions, one for each subscriber.

    3. Close ViewSubscriptions.sql.

    Lesson 11: Submitting Events to the Weather Application

    In this step, you will use a Transact-SQL script to submit events to the Weatherapplication, and then view the resulting notifications.

    Event Data

    Events are data that your notification application uses to generate notifications. Thenotification generation queries you write join event data and subscription data to producenotifications, such as the query in Lesson 5

    The queries that generate notifications are fired during each generator quantum that hasdata to process. Event rules run in any quantum in which one or more event batches

    http://msdn.microsoft.com/en-us/library/ms144261(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms144261(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms144261(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms144261(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms144261(v=SQL.90).aspx
  • 8/3/2019 Notification Services Tutorial

    33/36

    arrived. Scheduled rules run in any quantum that has scheduled subscriptions expected tobe processed.

    If there is no event data, there is no reason to run rules. Therefore, no notifications areproduced.

    WeatherData Events

    For this application, you will submit a batch of events using the event-submission storedprocedures. First, you will use the NSEventBeginBatchWeatherData stored procedureto open an event batch. You will specify the WeatherSPEventProvider non-hosted eventprovider as the event provider, which you defined in Lesson 6.

    Caution:

    Do not run these statements individually. They open and close an event batch, and mustrun in the same transaction. To run these queries, run the AddWeatherEvents.sql script, as

    documented later in this lesson.Copy-- Use the weather application's database.USE TutorialWeather;-- Start an event batch.DECLARE @BatchID bigint;EXEC dbo.NSEventBeginBatchWeatherData N'WeatherSPEventProvider',@BatchID OUTPUT;

    After creating the event batch, you use the NSEventWriteWeatherData storedprocedure to add three weather events. You must provide the event batch ID and thenprovide values for the event fields. This is one sample event:

    Copy-- Submit events.EXEC dbo.NSEventWriteWeatherData

    @EventBatchId=@BatchID,@City=N'Seattle',@Date=CONVERT(DATETIME, '2005-04-20 13:00:00', 120),@Low = 31,@High = 52,@Forecast=N'Sunny'

    After you use this stored procedure to submit three events, you will use theNSEventFlushBatchWeatherData to close the event batch and submit it to theapplication database.

    Copy-- Flush event batch.EXEC dbo.NSEventFlushBatchWeatherData @BatchID;

    You can display the event batch ID so that you can use views and stored procedures toview information about the event batch.

  • 8/3/2019 Notification Services Tutorial

    34/36

    Copy-- Display event batch IDSELECT @BatchID 'Event Batch';

    Submit Events to the Weather Application

    Use the AddWeatherEvents.sql query to add events to the Weather application.

    To submit events to the Weather application

    1. In Solution Explorer, expand Weather, expand Queries, and then openAddWeatherEvents.sql.

    2. Review the Transact-SQL code.3. Press F5 to run the query.

    You should receive two result sets. The first result set returns an EventCountvalue of 3. The second result set returns an event batch ID of 1.

    View Event Data

    Use the ViewWeatherEvents.sql query to view the events you just added.

    To view event data

    1. In Solution Explorer, open ViewWeatherEvents.sql.2. Review the Transact-SQL code.3. Press F5 to run the query.

    If you submitted multiple event batches, change the EventBatchId parameter asappropriate.

    View Notifications

    After about 30 seconds, Notification Services should have created a FileNotifications.htmfile in the Notifications folder. Open this file to view the notification data. You shouldreceive three notifications similar to the following:

    Notification Id: 1 Notification Class Name: WeatherNotifications Subscriber Id:stephanie Device Address: [email protected] Protocol Fields:

    Body: Weather report for the city ofSeattle [2/21/2005 2:01:00 PM]: SunnyExpected low temperature : 31 F.Expected high temperature: 52 F.For more information, visit our web site athttp://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=SeattleThank you for using SQL Server Notification Services.

    Notification Id: 2 Notification Class Name: WeatherNotifications Subscriber Id:david Device Address: [email protected] Protocol Fields: Body:

  • 8/3/2019 Notification Services Tutorial

    35/36

    Weather report for the city ofOrlando [2/22/2005 2:01:00 AM]: Partly CloudyExpected low temperature : 59 F.Expected high temperature: 81 F.For more information, visit our web site athttp://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=Orlando

    Thank you for using SQL Server Notification Services. Notification Id: 3 Notification Class Name: WeatherNotifications Subscriber Id:

    richard Device Address: [email protected] Protocol Fields: Body:Weather report for the city ofSeattle [2/21/2005 2:01:00 PM]: SunnyExpected low temperature : 31 F.Expected high temperature: 52 F.For more information, visit our web site athttp://www.msnbc.com/news/wea_front.asp?ta=y&tab=BW&tp=&czstr=SeattleThank you for using SQL Server Notification Services.

    Because you are using the built-in File delivery protocol, all three notifications generated

    from the events are in one file. If you submit more events, the additional notificationswill be appended to this file.

    Also notice that there is a header for each notification. This shows you information aboutthe notification, including the subscriber ID and device address. The actual text of thenotification starts after "Body:".

    Troubleshooting

    If you do not see the FileNotifications.htm file in the Notifications folder within oneminute, look in the Application log in Windows Event Viewer for events from

    Notification Services. The Windows service may not have permission to write to theNotifications folder.

    You can view information about any notifications generated for theWeatherNotifications notification class by running the ViewNotifications.sql query.

    To view notification information

    1. In Solution Explorer, expand Weather, expand Queries, and then double-clickViewNotifications.sql.

    2. Press F5 to run the query.

    Look in the DeliveryStatusDescription column for information aboutnotification delivery. If there are any failures, additional information should beprovided in the Application log.

    3. Close ViewSubscribersAndDevices.sql.

    What's Next?

  • 8/3/2019 Notification Services Tutorial

    36/36

    This tutorial has shown you how to build a simple prototype for a Notification Servicesapplication. To continue learning about Notification Services, use the followingresources:

    Building Notification Solutions

    SQL Server Notification Services Samples

    http://msdn.microsoft.com/en-us/library/ms171065(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms160889(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms171065(v=SQL.90).aspxhttp://msdn.microsoft.com/en-us/library/ms160889(v=SQL.90).aspx