Template Script: Service Broker\05 Tutorial Receiving Request.sql

/* ************* Lesson 5: Receiving a Request and Sending a Reply ************* */
-- Switch to the TargetDB database

--Then, run it to switch context to the DBA database where you will receive the request message and send a reply message back to the DBA.

USE DBA;
GO
-- Receive the request and send a reply

--Then, run it to receive the reply message from the SysMonTargetQueue and send a reply message back to the initiator. The RECEIVE statement retrieves the request message. Then, the following SELECT statement displays the text so that you can verify that it is the same message that was sent in the previous step. The IF statement tests whether the received message is a request message type, and if a SEND statement is used to send a reply message back to the initiator. The END CONVERSATION statement is used to end the target side of the conversation. The final SELECT statement displays the text of the reply message.

DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
DECLARE @RecvReqMsg NVARCHAR(100);
DECLARE @RecvReqMsgName SYSNAME;

BEGIN TRANSACTION;

WAITFOR
( RECEIVE TOP(1)
    @RecvReqDlgHandle = conversation_handle,
    @RecvReqMsg = message_body,
    @RecvReqMsgName = message_type_name
  FROM SysMonTargetQueue
), TIMEOUT 1000;

SELECT @RecvReqMsg AS ReceivedRequestMsg;

IF @RecvReqMsgName = N'//DBA/SysMon/SysMonRequestMessage'
BEGIN
     DECLARE @ReplyMsg NVARCHAR(100);
     SELECT @ReplyMsg =
        N'Message for Initiator service.';

     SEND ON CONVERSATION @RecvReqDlgHandle
          MESSAGE TYPE [//DBA/SysMon/SysMonReplyMessage]
          (@ReplyMsg);

     END CONVERSATION @RecvReqDlgHandle;
END

SELECT @ReplyMsg AS SentReplyMsg;

COMMIT TRANSACTION;
GO

SELECT * FROM SysMonTargetQueue

Description for Template Script: Service Broker\05 Tutorial Receiving Request.sql

Todo
Site Map | Printable View | © 2008 - 2012 NuRoN Consulting, INC | Powered by mojoPortal | HTML 5 | CSS | Original design by Andreas Viklund
Share This Using Popular Bookmarking Services