Delving Deeper into Notification Services

In this second part of a multi-part article covering the Notification Services framework, you will learn how to create a Notification Services instance and application, a delivery channel, and more. It is excerpted from chapter 18 of the book Programming SQL Server 2005, written by Bill Hamilton (O'Reilly, 2006; ISBN: 0596004796). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 15, 2007
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Creating a Notification Services Instance and Application

The Notification Services instance named StockWatch and a Notification Services application named StockWatchApp are created in theMain()method of Example 18-1. The code follows—the code that creates the Notification Services instance and application is highlighted:

  static void Main(string[] args)
  {
    
Server server = new Server("(local)");

                          


                 Figure 18-8.  StockWatch notification email

     // create a new instance
    NotificationServices ns = server.NotificationServices;
    nsi = new Instance(ns, "StockWatch");

    CreateDeliveryChannel(); 

    // create a new application in the StockWatch instance
    a = new Application(nsi, "StockWatchApp");
    a.BaseDirectoryPath = baseDirectoryPath;

    CreateEventClass();
    CreateSubscriptionClass();
    CreateNotificationClass();
    CreateHostedEventProvider();
    CreateGenerator();
    CreateDistributor();
    CreateVacuumSchedule();
 

    a.QuantumDuration = new TimeSpan(0, 0, 15);
    a.PerformanceQueryInterval = new TimeSpan(0, 0, 5);
    a.SubscriptionQuantumLimit = 1;
    a.ChronicleQuantumLimit = 1;
    a.VacuumRetentionAge = new TimeSpan(0, 0, 1);

    nsi.Applications.Add(a);

    Console.WriteLine("Added application.");

   
nsi.Create();
    nsi.RegisterLocal(serviceUserName, servicePassword);
    nsi.Enable();

    Console.WriteLine("Application enabled." + Environment.NewLine); 

    CreateSubscriber();
   
CreateSubscription();

    Console.WriteLine(Environment.NewLine + "Press any key to continue.");
   
Console.ReadKey();
  }
 

TheNotificationServicesobject represents a Notification Services server. TheInstancesproperty of theNotificationServices class returns anInstanceCollectionobject containing a collection of Notification Services instances asInstance objects. At a minimum, you must define aDeliveryChannel object and anApplicationobject to create anInstanceobject—in this example, this is done by theCreateDeliveryChannel()andMain()methods.

TheRegisterLocal()method of theInstance class registers an instance of Notification Services on the local computer. This is the same as registering a Notification Services instance using SQL Server Management Studio by right-clicking the Notification Services instance and selecting Tasks -> Register from the context menu. You can also register a Notification Services instance by using thenscontrol registercommand. Registering an instance creates or updates registry entries for the instance, creates performance counters, and optionally creates a Windows Service to run the instance.

TheEnable()method of theInstanceclass enables all instance and application components, allowing event collection, notification generation, notification distribution, and subscription management. ANotificationServicesinstance is disabled when you create it.

The NMO classes used to manage Notification Services instances are described in Table 18-1.

Table 18-1. NMO classes for managing Notification Services instances

Class Description
Instance Represents a Notification Services instance.
InstanceCollection Represents a collection of instances asInstanceobjects. TheInstancesproperty of the NotificationServicesclass returns the Notification Services instances on the server.

TheApplicationobject represents a Notification Services application. At a minimum, you must define aGeneratorobject and aDistributorobject for anApplicationobject—in this example, this is done by theCreateGenerator()andCreateDistributor()methods.

This example configures theApplicationobject by setting the following properties:

BaseDirectoryPath
  
Specifies the base directory path for the ADF

QuantumDuration
  
Specifies how frequently the generator tries to
   process work

PerformanceQueryInterval
  
Specifies how frequently the application updates its
   performance counters

SubscriptionQuantumLimit
  
Specifies how far the logical clock can fall behind the
   real-time clock before skipping subscription rule
   firings

ChronicleQuantumLimit
  
Specifies how far the logical clock can fall behind the
   real-time clock before skipping event chronicle firings

VacuumRetentionAge
  
Specifies the minimum age at which event and
   notification data is considered obsolete and can be
   removed

The NMO classes used to manage Notification Services applications are described in Table 18-2.

Table 18-2. NMO classes for managing applications

Class Description
Application Represents a Notification Services application.
ApplicationCollection Represents a collection of Notification Services applications asApplicationobjects. The Applicationsproperty of theInstanceclass returns the Notification Services applications hosted on the Notification Services instance.

Creating a Delivery Channel

A file delivery channel named StockWatchFileDeliveryChannel and an email delivery channel named StockWatchEmailDeliveryChannel are created in the CreateDeliveryChannel()method of Example 18-1. The code follows:

  private static void CreateDeliveryChannel()
 
{
     DeliveryChannelArgument dca;
     // add file delivery channel
     DeliveryChannel dcFile =
       
new DeliveryChannel(nsi, "StockWatchFileDeliveryChannel");
     dcFile.ProtocolName = "File";
     dca = new DeliveryChannelArgument(dcFile, "FileName");
     dca.Value = baseDirectoryPath + @"\Notifications\FileNotifications.txt";
     dcFile.DeliveryChannelArguments.Add(dca);
     nsi.DeliveryChannels.Add(dcFile);
     Console.WriteLine("Added delivery channel: " + dcFile.Name);

     // add email delivery channel
     DeliveryChannel dcEmail =
       
new DeliveryChannel(nsi, "StockWatchEmailDeliveryChannel");
     dcEmail.ProtocolName = "SMTP";
     nsi.DeliveryChannels.Add(dcEmail);
     Console.WriteLine("Added delivery channel: " + dcEmail.Name);
 
}

You have to add at least one delivery channel to a Notification Services instance before creating it. TheProtocolNameproperty of theDeliveryChannel object must be set toSMTP,File, or the name of a custom delivery protocol.

The NMO classes used to manage delivery channels are described in Table 18-3.

Table 18-3. NMO classes for managing delivery channels

Delivery channel Description
DeliveryChannel Represents a delivery channel.
DeliveryChannelArgument Represents a name-value pair specifying delivery channel configuration and authentication information for the delivery service.
DeliveryChannelArgumentCollection Represents a collection of delivery channel arguments asDelivery-ChannelArgumentobjects. TheDeliveryChannelArguments property of theDeliveryChannelclass returns the delivery channel arguments for the delivery channel.
DeliveryChannelCollection Represents a collection of delivery channels asDeliveryChannel objects. TheDeliveryChannelsproperty of theInstanceclass returns the delivery channels for the Notification Services instance.

Creating an Event Class

An event class named StockWatchEvents is created in the CreateEventClass() method of Example 18-1. The code follows:

  private static void CreateEventClass()  
  {
     EventClass ec = new EventClass(a, "StockWatchEvents");

     EventField ef;
     ef = new EventField(ec, "Symbol");
     ef.Type = "nvarchar(6)";
     ec.EventFields.Add(ef);
     ef = new EventField(ec, "Price");
     ef.Type = "float";
     ec.EventFields.Add(ef);

     a.EventClasses.Add(ec);

     Console.WriteLine("Added event class: " + ec.Name);
  }

An event class represents a type of event used by a Notification Services application. The event class has two fields—Symbolof typenvarchar(6)andPriceof typefloat.

The NMO classes for managing event classes, fields, and chronicles are described inTable 18-4.

Table 18-4. NMO classes for managing events

Class Description
EventChronicle Represents an event chronicle.
EventChronicleCollection Represents a collection of event chronicles asEventChronicleobjects. TheEventChroniclesproperty of theEventClassclass returns the event chronicles for the event class.
EventChronicleRule Represents an event chronicle maintenance query that the generator runs.
EventClass Represents an event class.
EventClassCollection Represents a collection of event classes asEventClassobjects. TheEvent-Classesproperty of theApplicationclass returns the event classes for the Notification Services application.
EventField Represents a field in an event class schema.
EventFieldCollection Represents a collection of event class schema fields asEventFieldobjects. The EventFieldsproperty of theEventClassclass returns the event fields for the event class.

Creating a Subscription Class and Subscription Event Rule

A subscription class with a single subscription event rule is created in the CreateSubscriptionClassMethod() method of Example 18-1. The code follows:

CreateSubscriptionClassMethod( ) method of Example 18-1. The code follows:A subscription class with a single subscription event rule is created in the CreateSubscriptionClassMethod( ) method of Example 18-1. The code follows:

  private static void CreateSubscriptionClass()
  {
     SubscriptionClass sc = new SubscriptionClass(a, "StockWatchSubscriptions");

     SubscriptionField sf;
     sf = new SubscriptionField(sc, "DeviceName");
     sf.Type = "nvarchar(255)";
     sc.SubscriptionFields.Add(sf);
     sf = new SubscriptionField(sc, "SubscriberLocale");
     sf.Type = "nvarchar(10)";
     sc.SubscriptionFields.Add(sf);
     sf = new SubscriptionField(sc, "Symbol");
     sf.Type = "nvarchar(6)";
     sc.SubscriptionFields.Add(sf);
     sf = new SubscriptionField(sc, "Price");
     sf.Type = "float";
     sc.SubscriptionFields.Add(sf);

     SubscriptionEventRule ser =
        new SubscriptionEventRule(sc, "StockWatchSubscriptionsEventRule");
    
ser.Action = @"INSERT INTO StockWatchNotifications (" +
         "SubscriberId, DeviceName, SubscriberLocale, Symbol, Price) " +
         "SELECT s.SubscriberId, s.DeviceName, s.SubscriberLocale, " +
         
"e.Symbol, e.Price " +
         "FROM StockWatchEvents e, StockWatchSubscriptions s " +
         "WHERE e.Symbol = s.Symbol";

     ser.EventClassName = "StockWatchEvents";

     sc.SubscriptionEventRules.Add(ser);

     a.SubscriptionClasses.Add(sc);

     Console.WriteLine("Added subscription class: " + sc.Name);
  }

ASubscriptionClass object defines a type of subscription within a Notification Services application. TheSubscriptionField objects added to theSubscriptionobject represent fields in the subscription class schema. ASubscriptionChronicleobject lets you store subscription information outside of tables used by the subscription class—this example does not use a subscription chronicle.

The NMO classes for managing subscription chronicles, subscription classes, and subscription fields are described in Table 18-5.

Table 18-5. NMO classes for managing subscription chronicles, classes, and fields

Class Description
SubscriptionChronicle Represents a subscription chronicle.
SubscriptionChronicleCollection Represents a collection of subscription chronicles as SubscriptionChronicleobjects. TheSubscriptionChronicles property of theSubscriptionClassclass returns the subscription chronicles for the subscription class.
SubscriptionClass Represents a subscription class.
SubscriptionClassCollection Represents a collection of subscription classes asSubscriptionClass objects. TheSubscriptionClassesproperty of theApplication class returns the subscription classes for the Notification Services application.
 SubscriptionField Represents a field in the subscription class schema.
 SubscriptionFieldCollectionRepresents a collection of fields asSubscriptionFieldobjects. The SubscriptionFieldsproperty of theSubscriptionClassclass returns the fields for the subscription class schema.

ASubscriptionEventRule object represents a rule that uses T-SQL queries to generate notifications when event batches arrive. TheActionproperty represents the T-SQL query for theSubscriptionEventRuleobject. In this example, notifications are generated when the ticker symbol of an event matches the ticker symbol specified in a subscription.

The NMO classes for managing the different types of subscription rules are described in Table 18-6.

Table 18-6. NMO classes for managing subscription rules

Class

Description

SubscriptionCondition
EventRule

Represents a subscription rule that the generator runs against subscriptions that use conditions to generate notifications.

SubscriptionCondition
EventRuleCollection

Represents a collection of subscription condition event rules as SubscriptionConditionEventRule objects. The subscriptionConditionEvent-Rulesproperty of the SubscriptionClassclass returns the subscription condition event rules for the subscription class.

SubscriptionCondition
ScheduledRule

Represents a subscription rule that the generator runs against scheduled subscriptions that use conditions to generate notifications.

 SubscriptionCondition
ScheduledRuleCollection

Represents a collection of subscription condition scheduled rules as SubscriptionConditionScheduled-Ruleobjects. The SubscriptionCondition-ScheduledRulesproperty of the SubscriptionClassclass returns the subscription condition scheduled rules for the subscription class.

SubscriptionEventRule

Represents an event rule that contains simple (not conditional) actions.

SubscriptionEvent
RuleCollection

Represents a collection of subscription event rules as SubscriptionEventRuleobjects. The SubscriptionEventRulesproperty of the SubscriptionClassclass returns the subscription event rules for the subscription class. 

SubscriptionScheduled
Rule
Represents a scheduled rule that contains actions that do not use conditions to generate notifications.
SubscriptionScheduled
RuleCollection

Represents a collection of scheduled rules as SubscriptionScheduledRuleobjects. The SubscriptionScheduledRulesproperty of the SubscriptionClassclass returns the scheduled rules for the subscription class.

Please check back next week for the conclusion of this article.

blog comments powered by Disqus
MS SQL SERVER ARTICLES

- Idera Releases SQL Diagnostic Manager v7.1
- MS SQL Sever 2012 Launch, New Idera Release
- OpenText Azure Cloud Solution, Geminaire Raa...
- Melissa Data Releases MatchUp Tool for SQL S...
- Glovia`s G2 ERP Solution to Support SQL Serv...
- Upgrade Assistant for SQL Server 2012 Releas...
- Azure Update Features Several New Improvemen...
- NT OBJECTives SQL Invader Tool Offers Free V...
- SQL Server ODBC Driver for Red Hat Enterpris...
- Heroku Postgres: A New SQL Database-as-a-Ser...
- Idera Compliance Manager 3.5 and SQL Server ...
- Microsoft and Joyent Announce Node.js Window...
- How to Install Xampp on Windows XP
- SQL Server 2008 SP3 and HP Database Enterpri...
- How To Install Windows Azure

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials