DAT300
SQL Server Notification Services: Application DevelopmentKen HendersonTechnical Lead, SQL Server SupportMicrosoft [email protected]
Development Process
ASPASP.NETVB Script
C#, VB.NET
VB ScriptT-SQL
ScriptXMLT-SQL
EventsSubscriptionsSubscribersNotificationsMatch Rules
DefineSchemas
andRules
Web ApplicationSubscribers
SubscriptionsEvents
nscontrol create,
register, update
Buildthe
SMA
LoadSample
Data
Compile the application
Extend the platformBuild the foundation
XSLTC#
VB.NET
Event Providers,Content
Formatters,Delivery
Protocols
AddCustom
Components
OperationsT-SQL
PerfmonSQLNS reports
Integrate for end to
end testingTas
ksF
ocus
Ski
lls s
et
Demo Outline:Stock Quotes Notifications
More sources of events (SQL Provider)More sources of events (SQL Provider) Richer notifications (Content Formatter)Richer notifications (Content Formatter)
File System Watcher for eventsFile System Watcher for events Xslt content formatterXslt content formatter Email for notificationsEmail for notifications
Define Define SchemasSchemas
Test applicationTest applicationusing built-inusing built-in
featuresfeatures
Extend the Extend the platformplatform
Events, subscriptions, notificationsEvents, subscriptions, notifications Write Match ruleWrite Match rule
NS InstanceMakes it possible for two or more applications to share the same set of subscribers
Multithreaded Windows ServiceHosts threads for Event Providers, Generator, Distributor
Multiple Instances per computerTest and production
Instance may span multiplecomputers
E.g., multiple Distributors
Defined by an xml configuration file NewsNewsStockStock SportsSports
SubscribersSubscribers
StockInstance
NS ApplicationDefines the logic of your notification applicationOne or more per instanceEntirely defined by an application definition file
Schemas for Events, Subscriptions, NotificationsMatch rulesContent FormattersEvent providersDelivery protocols
Building The Foundation:Defining Schemas and Rules
Model your Events as rows in a SQL table
Model your subscriptions as rows in a table
Use the power of SQL to write your match rule
Events
StockSymbol
MSFT
Stock Value
41
ORCL 11 1/4
SUNW 9 1/2
INTC 22
IBM 65
CSCO 11 1/2
Subscriptions
StockSymbol
MSFT
Trigger
40
SubscriberId
patricet
Notifications
StockSymbol
MSFT
Stock Value
41
SubscriberId
patricet
<xsl:template match="notification"><b><xsl:value-of select="StockSymbol"/></b>is now trading at: <b>$<xsl:value-ofselect="StockValue"/></b></xsl:template>
Format the raw notification using a basic xslt
SELECT s.SubscriberId, e.StockSymbol, e.StockValueFROM Events e, Subscriptions sWHERE (e.StockSymbol = s.StockSymbol)AND (e.StockValue > s.Trigger)
Building The Foundation:’Compiling’ the application
nscontrol createReads config. / ADF xml files
Creates supporting databases, tables, views, stored procs…
nscontrol register (on every machine running the service)
Specify Windows/SQL credentials/encryption key
Installs a multi threaded Windows service per instance
Registers perfmon counters and SQLNS keys
nscontrol enableActivates EP, GP, DP
Start instance
EventProviderdisabled
Generatordisabled
Distributordisabled
EventProviderenabled
Generatorenabled
Distributorenabled
EventProviderenabled
Generatorenabled
Distributorenabled
Windows ServiceStopped
Windows ServiceStarted
ConfigConfig
InstanceInstance App1App1
App2App2
App3App3
ADF1ADF1ADF2ADF2
ADF3ADF3
Building The Foundation:Loading Sample Data
Load sample data:Subscribers, subscriptions, eventsPick data that will generate notifications!
Use built-in componentsFileSystemWatcher, xslt, smtp
How?Subscribers and subscriptions:
ASP, ASP.NET (unlikely at this stage of development, unless you are a UI god!)VB Script, using our object modelC++, C#, VB, VB.NET, using our object model
Events: using the File System Watcher providerC++, C#, VB, VB.NET, using our object model
NEVER insert data directly in the SQLNS tables!
Building The FoundationVBS scriptsVBS scripts
Data Data ChangeChange
ss
Data Data ChangeChange
ss
ExternalExternalDeliveryDelivery
ExternalExternalDeliveryDelivery
Event ProviderEvent Provider
EventsEvents
SubscribersSubscribersSubscriber DevicesSubscriber Devices
SubscriptionsSubscriptionsNotificationsNotifications
GeneratorGenerator DistributorDistributor
SQLSQLMatchMatchRuleRule
File System File System WatcherWatcher
EventEventProviderProvider
HostHost
XSLT XSLT CFCF
XSLTXSLT
EmailEmail
Building The Foundation
demodemo
Extending the platformVBS scriptsVBS scripts
Data Data ChangeChange
ss
Data Data ChangeChange
ss
ExternalExternalDeliveryDelivery
ExternalExternalDeliveryDelivery
Event ProviderEvent Provider
EventsEvents
SubscribersSubscribersSubscriber DevicesSubscriber Devices
SubscriptionsSubscriptionsNotificationsNotifications
GeneratorGenerator DistributorDistributor
SQLSQLMatchMatchRuleRule
File System File System WatcherWatcher
EventEventProviderProvider
HostHost
XSLT XSLT CFCF
XSLTXSLT
EmailEmail
SQL Server SQL Server providerprovider
Event Providers: Example: SQL Server Event Provider
Event ProvidersGet data into the Events tableBuilt-in: FileSystemWatcher, SQL Provider
SQL Server Event ProviderHosted by the Event Provider HostExecutes a SQL query on a schedule to retrieve changesStore changes as data in the events table
EventsEventsDataData
SourceSourceSQL Server SQL Server
providerprovider
EventEventProviderProvider
HostHost
QueryQueryPostQueryPostQueryScheduleSchedule
ChangesChanges
SQL Server Event Provider
Source Table
StockSymbol
MSFT
Stock Value
41
ORCL 11 1/4
SUNW 9 1/2
INTC 22
IBM 65
CSCO 11 1/2
LastUpdate
2002-10-12 1:02:05 pm
2002-10-12 1:02:05 pm
2002-10-12 1:02:06 pm
2002-10-12 1:02:02 pm
2002-10-12 1:02:07 pm
2002-10-12 1:02:04 pm
SQL Provider:Every 2 minutes, retrieve all
updated stocks
Events
StockSymbol
MSFT
Stock Value
41
ORCL 11 1/4
SUNW 9 1/2
INTC 22
IBM 65
CSCO 11 1/2
LastUpdate
2002-10-12 1:03:00 pm
2002-10-12 1:03:00 pm
2002-10-12 1:03:00 pm
2002-10-12 1:03:00 pm
2002-10-12 1:03:00 pm
2002-10-12 1:03:00 pm
MSFT 41 1/8
ORCL 11 1/8
SUNW 9
2002-10-12 1:03:25 pm
2002-10-12 1:03:32 pm
2002-10-12 1:04:06 pm
GeneratorGenerator
MSFT 41 1/8
ORCL 11 1/8
SUNW 9
2002-10-12 1:05:00 pm
2002-10-12 1:05:00 pm
2002-10-12 1:05:00 pm
Event Batch#1
Event Batch#2
SQL Server Provider
demodemo
Extending the platformVBS scriptsVBS scripts
Data Data ChangeChange
ss
Data Data ChangeChange
ss
ExternalExternalDeliveryDelivery
ExternalExternalDeliveryDelivery
Event ProviderEvent Provider
EventsEvents
SubscribersSubscribersSubscriber DevicesSubscriber Devices
SubscriptionsSubscriptionsNotificationsNotifications
GeneratorGenerator DistributorDistributor
SQLSQLMatchMatchRuleRule
File System File System WatcherWatcher
EventEventProviderProvider
HostHost
XSLT XSLT CFCF
XSLTXSLT
EmailEmail
SQL Server SQL Server providerprovider
Custom CFCustom CFCustom EPCustom EP
Content Formatter
Content Formatters Transform raw notification
Built-in: XSLT Content Formatter
Why a custom content formatter?Beyond simple xslt formatting (complex html, external formatting dependencies, compute operations)
Target multiple devices/languages within a single piece of code (vs. Multiple xslt files)
Performance
Managed code dll loaded by the distributor.Cached in memory (stop, disable, update, enable, start)
Exposes 3 methods: Initialize, FormatContent, Close
DistributorContent Formatting Example
NotificationsNotifications
SubscribersSubscribers
SubscriptionsSubscriptions
SubscriberSubscriberDevicesDevices
Notification FieldsCity: SeattleHighTemp: 54LowTemp: 45
Recipient InfoSubscriberId: patricetSubscriberLocale: en-USDeviceType: Phone
Forecast for Seattle is:54 F / 45 F
public string FormatContent(string subscriberLocale,string deviceTypeName,RecipientInfo recipientInfo,Hashtable[] rawContent)
{ string output;
foreach (Hashtable notification in rawContent) { if (subscriberLocale == “en-US”) { output = String.Format( "Forecast for {0} is: {1} F / {2} F",
notification [“City”],notification [“HighTemp”],notification [“LowTemp”]);
}else
{ output = String.Format( “Forecast for {0} is: {1} C / {2} C”,
notification [“City”], (notification [“HighTemp”]-32)/9, (notification [“LowTemp”]-32/9);
} }
return output;}
Content Formatter
demodemo
Extending the platformVBS scriptsVBS scripts
Data Data ChangeChange
ss
Data Data ChangeChange
ss
ExternalExternalDeliveryDelivery
ExternalExternalDeliveryDelivery
Event ProviderEvent Provider
EventsEvents
SubscribersSubscribersSubscriber DevicesSubscriber Devices
SubscriptionsSubscriptionsNotificationsNotifications
GeneratorGenerator DistributorDistributor
SQLSQLMatchMatchRuleRule
File System File System WatcherWatcher
EventEventProviderProvider
HostHost
XSLT XSLT CFCF
XSLTXSLT
EmailEmail
SQL Server SQL Server providerprovider
Custom CFCustom CFSMS DPSMS DPCustom EPCustom EP
.NET Alerts.NET Alerts **
FileFile
HttpExtHttpExt
What We’ve Done
More sources of events (SQL Provider)More sources of events (SQL Provider) Richer notifications (Content Formatter)Richer notifications (Content Formatter)
File System Watcher for eventsFile System Watcher for events Xslt content formatterXslt content formatter Email for notificationsEmail for notifications
Define Define SchemasSchemas
Test applicationTest applicationusing built-inusing built-in
featuresfeatures
Extend the Extend the platformplatform
Events, subscriptions, notificationsEvents, subscriptions, notifications Write Match ruleWrite Match rule
Out of the box vs. What you author
Subscription Management ApplicationSubscription Management Application
Data Data ChangeChange
ss
Data Data ChangeChange
ss
ExternalExternalDeliveryDelivery
ExternalExternalDeliveryDelivery
Event ProviderEvent Provider
EventsEvents
SubscribersSubscribersSubscriber DevicesSubscriber Devices
SubscriptionsSubscriptionsNotificationsNotifications
GeneratorGenerator DistributorDistributor
SQLSQLMatchMatchRuleRule
File System File System WatcherWatcher
SQL Server SQL Server providerprovider
Custom EPCustom EP
EventEventProviderProvider
HostHost
XSLT XSLT CFCF
Custom CFCustom CF
XSLTXSLT
Custom DPCustom DP
EmailEmail
.NET Alerts.NET Alerts **
FileFile
HttpExtHttpExt
Summary
Think of events, subscriptions and notifications as rows in SQL tables!
Start building the foundation before extending it
We want to hear from you: Come and talk to us about your application!
Community Resources
Community Resourceshttp://www.microsoft.com/communities/default.mspx
Most Valuable Professional (MVP)http://www.mvp.support.microsoft.com/
NewsgroupsConverse online with Microsoft Newsgroups, including Worldwidehttp://www.microsoft.com/communities/newsgroups/default.mspx
User GroupsMeet and learn with your peershttp://www.microsoft.com/communities/usergroups/default.mspx
evaluationsevaluations
© 2003 Microsoft Corporation. All rights reserved.© 2003 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.