SQL Server Performance

Trying To Learn Service Broker

Discussion in 'Getting Started' started by lcerni, Mar 30, 2008.

  1. lcerni New Member

    I am attempting to learn Service Broker. I was following the Tutorial on http://technet.microsoft.com/en-us/library/bb839488.aspx.
    However, I think I am doing something wrong.
    /***
    For this step, I am getting NULL for the output.

    DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
    DECLARE @RecvReqMsg NVARCHAR(100);
    DECLARE @RecvReqMsgName sysname;
    BEGIN TRANSACTION;
    RECEIVE TOP(1)
    @RecvReqDlgHandle = conversation_handle,
    @RecvReqMsg = message_body,
    @RecvReqMsgName = message_type_name
    FROM TargetQueue1DB;
    SELECT @RecvReqMsg AS ReceivedRequestMsg;
    IF @RecvReqMsgName =
    N'//AWDB/1DBSample/RequestMessage'
    BEGIN
    DECLARE @ReplyMsg NVARCHAR(100);
    SELECT @ReplyMsg =
    N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';

    SEND ON CONVERSATION @RecvReqDlgHandle
    MESSAGE TYPE
    [//AWDB/1DBSample/ReplyMessage]
    (@ReplyMsg);
    END CONVERSATION @RecvReqDlgHandle;
    END
    SELECT @ReplyMsg AS SentReplyMsg;
    COMMIT TRANSACTION;
    GO
    ---Output
    ReceivedRequestMsg
    NULL
    SentReplyMsg
    NULL
    ---
    ***/

    And for the last step
    /***

    DECLARE @RecvReplyMsg NVARCHAR(100);
    DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
    BEGIN TRANSACTION;
    RECEIVE TOP(1)
    @RecvReplyDlgHandle = conversation_handle,
    @RecvReplyMsg = message_body
    FROM InitiatorQueue1DB;
    END CONVERSATION @RecvReplyDlgHandle;
    SELECT @RecvReplyMsg AS ReceivedReplyMsg;
    COMMIT TRANSACTION;
    GO
    /***
    Results---
    ReceivedReplyMsg
    NULL

    Messages---
    (0 row(s) affected)
    Msg 8418, Level 16, State 1, Line 11
    The conversation handle is missing. Specify a conversation handle.
    (1 row(s) affected)

    ---Output
    Results---
    ReceivedReplyMsg
    NULL

    Messages---
    (0 row(s) affected)
    Msg 8418, Level 16, State 1, Line 11
    The conversation handle is missing. Specify a conversation handle.
    (1 row(s) affected)
    ---
    ***/

    I don't understand what I am missing. I have tried to see it but I don't. Can someone explain to me what I am doing wrong?
    Below is all the code:

    ---------------------------------------------------------------------
    --http://technet.microsoft.com/en-us/library/bb839488.aspx
    CREATE MESSAGE TYPE
    [//AWDB/1DBSample/RequestMessage]
    VALIDATION = WELL_FORMED_XML;
    CREATE MESSAGE TYPE
    [//AWDB/1DBSample/ReplyMessage]
    VALIDATION = WELL_FORMED_XML;
    GO
    ---
    CREATE CONTRACT [//AWDB/1DBSample/SampleContract]
    ([//AWDB/1DBSample/RequestMessage]
    SENT BY INITIATOR,
    [//AWDB/1DBSample/ReplyMessage]
    SENT BY TARGET
    );
    GO
    ---
    CREATE QUEUE TargetQueue1DB;
    CREATE SERVICE
    [//AWDB/1DBSample/TargetService]
    ON QUEUE TargetQueue1DB
    ([//AWDB/1DBSample/SampleContract]);
    GO
    ---
    CREATE QUEUE InitiatorQueue1DB;
    CREATE SERVICE
    [//AWDB/1DBSample/InitiatorService]
    ON QUEUE InitiatorQueue1DB;
    GO
    ---
    DECLARE @InitDlgHandle UNIQUEIDENTIFIER;
    DECLARE @RequestMsg NVARCHAR(100);
    BEGIN TRANSACTION;
    BEGIN DIALOG @InitDlgHandle
    FROM SERVICE
    [//AWDB/1DBSample/InitiatorService]
    TO SERVICE
    N'//AWDB/1DBSample/TargetService'
    ON CONTRACT
    [//AWDB/1DBSample/SampleContract]
    WITH
    ENCRYPTION = OFF;
    SELECT @RequestMsg =
    N'<RequestMsg>Message for Target service.</RequestMsg>';
    SEND ON CONVERSATION @InitDlgHandle
    MESSAGE TYPE
    [//AWDB/1DBSample/RequestMessage]
    (@RequestMsg);
    SELECT @RequestMsg AS SentRequestMsg;
    COMMIT TRANSACTION;
    GO
    /***
    SentRequestMsg
    <RequestMsg>Message for Target service.</RequestMsg>
    ***/
    ---
    DECLARE @RecvReqDlgHandle UNIQUEIDENTIFIER;
    DECLARE @RecvReqMsg NVARCHAR(100);
    DECLARE @RecvReqMsgName sysname;
    BEGIN TRANSACTION;
    RECEIVE TOP(1)
    @RecvReqDlgHandle = conversation_handle,
    @RecvReqMsg = message_body,
    @RecvReqMsgName = message_type_name
    FROM TargetQueue1DB;
    SELECT @RecvReqMsg AS ReceivedRequestMsg;
    IF @RecvReqMsgName =
    N'//AWDB/1DBSample/RequestMessage'
    BEGIN
    DECLARE @ReplyMsg NVARCHAR(100);
    SELECT @ReplyMsg =
    N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';

    SEND ON CONVERSATION @RecvReqDlgHandle
    MESSAGE TYPE
    [//AWDB/1DBSample/ReplyMessage]
    (@ReplyMsg);
    END CONVERSATION @RecvReqDlgHandle;
    END
    SELECT @ReplyMsg AS SentReplyMsg;
    COMMIT TRANSACTION;
    GO
    /***
    ReceivedRequestMsg
    NULL
    SentReplyMsg
    NULL
    ***/
    ---
    DECLARE @RecvReplyMsg NVARCHAR(100);
    DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
    BEGIN TRANSACTION;
    RECEIVE TOP(1)
    @RecvReplyDlgHandle = conversation_handle,
    @RecvReplyMsg = message_body
    FROM InitiatorQueue1DB;
    END CONVERSATION @RecvReplyDlgHandle;
    SELECT @RecvReplyMsg AS ReceivedReplyMsg;
    COMMIT TRANSACTION;
    GO
    /***
    Results---
    ReceivedReplyMsg
    NULL

    Messages---
    (0 row(s) affected)
    Msg 8418, Level 16, State 1, Line 11
    The conversation handle is missing. Specify a conversation handle.
    (1 row(s) affected)
    ***/
    ---

    ---------------------------------------------------------------------
  2. lcerni New Member

  3. FrankKalis Moderator

    Check out the Apress book by Klaus Aschenbrenner on Service Broker.

Share This Page