Share via


Lesson 6: Receiving the Reply and Ending the Conversation

In this lesson, you will learn to receive the reply message from the target service and end the conversation. Run these steps from a copy of Management Studio that is running on the same computer as the initiator instance the Database Engine.

Procedures

Switch to the InitiatorDB database

  • Copy and paste the following code into a Query Editor window. Then, run it to switch context back to the InstInitiatorDB database where you will receive the reply message and end the conversation.

    USE InstInitiatorDB;
    GO
    

Receive the reply and end the conversation

  • Copy and paste the following code into a Query Editor window. Then, run it to receive the reply message and end the conversation. The RECEIVE statement retrieves the reply message from the InstInitiatorQueue. The END CONVERSATION statement ends the initiator side of the conversation. The last SELECT statement displays the text of the reply message so that you can confirm it is the same as what was sent in the last step.

    DECLARE @RecvReplyMsg NVARCHAR(100);
    DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER;
    
    BEGIN TRANSACTION;
    
    WAITFOR
    ( RECEIVE TOP(1)
        @RecvReplyDlgHandle = conversation_handle,
        @RecvReplyMsg = message_body
      FROM InstInitiatorQueue
    ), TIMEOUT 1000;
    
    END CONVERSATION @RecvReplyDlgHandle;
    
    -- Display recieved request.
    SELECT @RecvReplyMsg AS ReceivedReplyMsg;
    
    COMMIT TRANSACTION;
    GO
    

Next Steps

This concludes the tutorial. Tutorials are brief introductions only. They do not describe all available options. Tutorials use simplified logic and error handling, and should not be used in a production environment. To create efficient, reliable, and robust conversations, you need more complex code than the example in this tutorial.

Return To The Service Broker Tutorials

Service Broker Tutorials