Using a SOAP Header to Subscribe to Query Notifications
HTTP endpoints support notification requests through the <notificationRequest> header element. A <notificationRequest> element contains the attributes shown in the following table.
The message string for the request.
The name of the Service Broker service to deliver notifications to.
An optional time-out for the request.
For more information on the schema for the <notificationRequest> element, see SOAP Request Message Structure.
To subscribe to a query notification using SOAP, include the <notificationRequest> element in the SOAP header. For example, the following header creates a notification subscription with a timeout of 10000. Notification messages are delivered to the Service Broker service WebCacheNotifications.
<SOAP-ENV:Header> <sqloptions:notificationRequest notificationId="NotificationMessage" timeout="10000" deliveryService="WebCacheNotifications"> </sqloptions:notificationRequest> </SOAP-ENV:Header>
Subscribing to a query notification using the <notificationRequest> element requires that you prepare the underlying Service Broker objects before your application can request the notification. Once you request the subscription, your application monitors the queue for a notification message and reacts appropriately when the message arrives.
The example below creates a service named WebCacheMessages that uses the queue WebCacheNotifications and then creates a route to the WebCacheMessages service in the local database.
USE AdventureWorks2008R2 ; CREATE QUEUE WebSiteCacheMessages ; CREATE SERVICE WebCacheNotifications ON QUEUE WebSiteCacheMessages ([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]) ; CREATE ROUTE WebCacheMessagesRoute WITH SERVICE_NAME = 'WebCacheNotifications', ADDRESS = 'LOCAL' ;
The contract http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification specifies that messages of type http://schemas.microsoft.com/SQL/Notifications/QueryNotification may be sent by the initiator of the conversation.
SQL Server delivers query notifications using Service Broker. A query notification message has the message type name http://schemas.microsoft.com/SQL/Notifications/QueryNotification. Service Broker validates messages of this type as VALID_XML WITH SCHEMA COLLECTION. The application is responsible for monitoring the queue and processing the notification messages. Notice that you can retrieve messages from a queue using SOAP messages. You can also write a stored procedure or an external program to process the notification messages.
For more information on creating an application to process query notification messages, see Benefits of Programming with Service Broker.