本文演示的是同一个SQL Server中不同数据库之间的基于Service Broker的异步消息传递,其中Stored Procedure充当Service Program。
3.在Initiator端发送消息
Use DotNetFun2
go
DECLARE @conversationHandle uniqueidentifier
BEGIN TRANSACTION
-- Begin a dialog to the Hello World Service
BEGIN DIALOG @conversationHandle
FROM SERVICE [SendingService]
TO SERVICE 'ReceivingService','
a727462b-52e7-4405-9eee-d19923729790'
ON CONTRACT [XMLContract]
WITH ENCRYPTION = OFF, LIFETIME = 600;
-- Send message
SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE [XMLMessage]
('<hello>Welcome to Rickie Lee''s blog,
www.cnblogs.com/rickie</hello>');
Select * From sys.conversation_endpoints
COMMIT |
其中,TO SERVICE 'ReceivingService','a727462b-52e7-4405-9eee-d19923729790',’ReceivingSerice’表示目标Service名称,'a727462b-52e7-4405-9eee-d19923729790'则指定目标Service所在的数据库,可以通过如下SQL Script获取:
-- Retrieve remote broker instance guid
SELECT service_broker_guid
FROM sys.databases
WHERE database_id = DB_ID('HelloWorldDB') |
另外,可以通过如下的SQL script来检测Initiator端收到的Reply消息:
Select cast(message_body as XML) From SendingQueue
Receive message_type_name,
cast(message_body as XML)
From SendingQueue |
4.查询对话端点状态(State of Conversation Endpoints)
最后,你可以通过在Target/Initiator端查询sys.conversation_endpoints表,获取Dialog对话状态:
Select * From sys.conversation_endpoints |