Delving Deeper into Notification Services
(Page 1 of 4 )
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.
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. |
Next: Creating a Delivery Channel >>
More MS SQL Server Articles
More By O'Reilly Media
|
This article is excerpted from chapter 18 of the book Programming SQL Server 2005, written by Bill Hamilton (O'Reilly, 2006; ISBN: 0596004796). Check it out today at your favorite bookstore. Buy this book now.
|
|