使用 SOAP 标头订阅查询通知

HTTP 端点支持通过 <notificationRequest> 标头元素进行通知请求。<notificationRequest> 元素包含下表所示的属性。

属性

类型

说明

notificationId

text

请求的消息字符串。

deliveryService

text

向其传递通知的 Service Broker 服务的名称。

timeout

int

请求的可选超时值。

有关 <notificationRequest> 元素的架构的详细信息,请参阅 SOAP 请求消息结构

若要使用 SOAP 订阅查询通知,请在 SOAP 标头中包含 <notificationRequest> 元素。例如,下面的标头创建超时值为 10000 的通知订阅。通知消息被传递到 Service Broker 服务 WebCacheNotifications

     <SOAP-ENV:Header>
         <sqloptions:notificationRequest  notificationId="NotificationMessage"
             timeout="10000"   deliveryService="WebCacheNotifications">
         </sqloptions:notificationRequest>
      </SOAP-ENV:Header>

使用 <notificationRequest> 元素订阅查询通知需要在应用程序请求该通知之前准备好 Service Broker 基础对象。一旦您请求订阅,您的应用程序就将监视队列中是否有通知消息,并在收到消息后做出适当的响应。

下面的示例先创建使用队列 WebCacheNotifications 的名为 WebCacheMessages 的服务,然后在本地数据库中创建指向 WebCacheMessages 服务的路由。

USE AdventureWorks2008R2 ;

CREATE QUEUE WebSiteCacheMessages ;

CREATE SERVICE WebCacheNotifications
  ON QUEUE WebSiteCacheMessages
  ([https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]) ;

CREATE ROUTE
  WebCacheMessagesRoute
  WITH SERVICE_NAME = 'WebCacheNotifications',
       ADDRESS = 'LOCAL' ;

约定 https://schemas.microsoft.com/SQL/Notifications/PostQueryNotification 指定会话的发起方可以发送 https://schemas.microsoft.com/SQL/Notifications/QueryNotification 类型的消息。

SQL Server 使用 Service Broker 传递查询通知。查询通知消息的消息类型名称为 https://schemas.microsoft.com/SQL/Notifications/QueryNotification。Service Broker 按 VALID_XML WITH SCHEMA COLLECTION 验证此类型的消息。该应用程序负责监视队列和处理通知消息。请注意,可以使用 SOAP 消息从队列检索消息。还可以编写存储过程或外部程序来处理通知消息。

有关创建用于处理查询通知消息的应用程序的详细信息,请参阅用 Service Broker 编程的优点