发起会话示例
DECLARE @InitDlgHandle UNIQUEIDENTIFIER; --定义句柄
BEGIN DIALOG @InitDlgHandle --开启会话
FROM SERVICE [//AWDB/1DBSample/InitiatorService] --指定从哪个服务到哪个服务
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract] --会话中使用的约定
WITH ENCRYPTION = OFF; --不加密会话
--
--14.3.3节示例
--
发起会话并发送消息
DECLARE @InitDlgHandle UNIQUEIDENTIFIER,@Msg xml;
BEGIN DIALOG @InitDlgHandle --开启会话
FROM SERVICE [//AWDB/1DBSample/InitiatorService]
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract]
WITH ENCRYPTION = OFF;
--以上是发起会话,接下来就是在该会话中发送消息
SET @Msg='<Say>Hello World</Say>'; --定义发生消息的内容
SEND ON CONVERSATION @InitDlgHandle --在会话中发生消息
MESSAGE TYPE [//AWDB/1DBSample/RequestMessage](@Msg); --指定消息类型
接收消息并打印
DECLARE @Msg nvarchar(100);
RECEIVE TOP(1)
@Msg=convert(nvarchar(100),message_body)
FROM Queue1
PRINT @Msg
接收消息放入表变量
DECLARE @tbMsg table(msg nvarchar(100),receiveTime datetime)
WAITFOR( --不超时的等待接收消息
RECEIVE TOP(1) --接收一条消息
convert(nvarchar(100),message_body),getdate()
FROM Queue1 --消息队列
INTO @tbMsg --将消息放入变量
)
SELECT * FROM @tbMsg --查询接收到的消息
--
--14.3.4节示例
--
将一个会话添加到会话组
DECLARE @A UNIQUEIDENTIFIER,@B UNIQUEIDENTIFIER;
BEGIN DIALOG @A --开启一个会话
FROM SERVICE [//AWDB/1DBSample/InitiatorService]
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract];
--接下来再开启一个会话
BEGIN DIALOG @B
FROM SERVICE [//AWDB/1DBSample/InitiatorService]
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract]
WITH RELATED_CONVERSATION=@A --说明与第一个会话是同一个会话组
指定会话组ID创建会话组和添加会话
DECLARE @A UNIQUEIDENTIFIER,@B UNIQUEIDENTIFIER,@GroupID UNIQUEIDENTIFIER;
SET @GroupID=NEWID(); --定义会话组的标识
BEGIN DIALOG @A --开启一个会话
FROM SERVICE [//AWDB/1DBSample/InitiatorService]
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract]
WITH RELATED_CONVERSATION_GROUP=@GroupID; --创建一个会话组
--接下来再开启一个会话
BEGIN DIALOG @B
FROM SERVICE [//AWDB/1DBSample/InitiatorService]
TO SERVICE '//AWDB/1DBSample/TargetService'
ON CONTRACT [//AWDB/1DBSample/SampleContract]
WITH RELATED_CONVERSATION_GROUP=@GroupID; --将会话添加到会话组中
锁定Queue1队列中的下一会话组
DECLARE @conversation_group_id UNIQUEIDENTIFIER ;
WAITFOR (
GET CONVERSATION GROUP @conversation_group_id --锁定队列中的下一会话组
FROM Queue1
) ;
--
--14.3.5节示例
--
启用Service Broker
USE master;
GO
ALTER DATABASE BrokerTest1
SET ENABLE_BROKER; --启用Service Broker
创建消息类型
USE BrokerTest1;
GO
CREATE MESSAGE TYPE [//BrokerTest1/Sample1/RequestMessage] --创建发生消息类型
CREATE MESSAGE TYPE [//BrokerTest1/Sample1/ReplyMessage] --创建接收消息类型
创建约定
CREATE CONTRACT [//BrokerTest1/Sample1/Contract1]
--创建约定指定发送类型和接收类型
(
[//BrokerTest1/Sample1/RequestMessage] SENT BY INITIATOR,
[//BrokerTest1/Sample1/ReplyMessage] SENT BY TARGET
);
创建目标队列和服务
CREATE QUEUE TargetQueue; --创建目标队列
GO
CREATE SERVICE [//BrokerTest1/Sample1/TargetService] --创建服务
ON QUEUE TargetQueue([//BrokerTest1/Sample1/Contract1]);
GO;
创建发起方队列和服务
CREATE QUEUE InitiatorQueue; --创建发起方队列
GO
CREATE SERVICE [//BrokerTest1/Sample1/InitiatorService] --创建发起方服务
ON QUEUE InitiatorQueue;
发起会话并发送消息
DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
BEGIN DIALOG @InitDlgHandle --开启会话
FROM SERVICE [//BrokerTest1/Sample1/InitiatorService]
TO SERVICE N'[//BrokerTest1/Sample1/TargetService]'
ON CONTRACT [//BrokerTest1/Sample1/Contract1]
;--这个分号是必须的
SEND ON CONVERSATION @InitDlgHandle --在会话中发送消息
MESSAGE TYPE [//BrokerTest1/Sample1/RequestMessage](N'ZengYi'); --发生消息内容
接收消息并发送答复
DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName sysname;
WAITFOR --等待接收消息
( RECEIVE TOP(1) --接收一条消息
@RecvReqDlgHandle = conversation_handle, 将接收的消息赋给变量
@RecvReqMsg = message_body,
@RecvReqMsgName = message_type_name
FROM TargetQueue);
IF @RecvReqMsgName = N'//BrokerTest1/Sample1/RequestMessage' --判断消息正确性
BEGIN
DECLARE @ReplyMsg NVARCHAR(100);
SET @ReplyMsg = @RecvReqMsg+N',Hello World'; --回复消息
SEND ON CONVERSATION @RecvReqDlgHandle
MESSAGE TYPE [//BrokerTest1/Sample1/ReplyMessage](@ReplyMsg);
END CONVERSATION @RecvReqDlgHandle;
END
处理答复并结束会话
DECLARE @RecvReplyMsg NVARCHAR(100);
DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
WAITFOR --等待回复
( RECEIVE TOP(1) --读取一条回复
@RecvReplyDlgHandle = conversation_handle,
@RecvReplyMsg = message_body
FROM InitiatorQueue
);
END CONVERSATION @RecvReplyDlgHandle; --结束会话
PRINT @RecvReplyMsg --输出回复的内容