Edit

Share via


Lesson 5: Receive a request and send a reply

Applies to: SQL Server Azure SQL Managed Instance

In this lesson, you learn how to receive a request message from the target queue and send a reply message to the initiator service. Run these steps from a copy of Management Studio that's running on the same computer as the target instance of the Database Engine.

Procedures

Switch to the TargetDB database

  • Copy and paste the following code into a Query Editor window, then run it to switch context to the InstTargetDB database where you receive the request message and send a reply message back to the InstInitiatorDB.

    USE InstTargetDB;
    GO
    

Receive the request and send a reply

  • Copy and paste the following code into a Query Editor window. Then, run it to receive the reply message from the InstTargetQueue 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's 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 AS UNIQUEIDENTIFIER;
    DECLARE @RecvReqMsg AS NVARCHAR (100);
    DECLARE @RecvReqMsgName AS sysname;
    
    BEGIN TRANSACTION;
    
    WAITFOR (RECEIVE TOP (1) @RecvReqDlgHandle = conversation_handle,
        @RecvReqMsg = message_body,
        @RecvReqMsgName = message_type_name FROM InstTargetQueue),
    TIMEOUT 1000;
    
    SELECT @RecvReqMsg AS ReceivedRequestMsg;
    
    IF @RecvReqMsgName = N'//BothDB/2InstSample/RequestMessage'
        BEGIN
            DECLARE @ReplyMsg AS NVARCHAR (100);
            SELECT @ReplyMsg = N'<ReplyMsg>Message for Initiator service.</ReplyMsg>';
            SEND ON CONVERSATION (@RecvReqDlgHandle)
                MESSAGE TYPE [//BothDB/2InstSample/ReplyMessage] (@ReplyMsg);
            END CONVERSATION @RecvReqDlgHandle;
        END
    
    SELECT @ReplyMsg AS SentReplyMsg;
    
    COMMIT TRANSACTION;
    GO
    

Next step

You've successfully received the request message and sent a reply message to the initiator service. Next, you receive the reply message from the initiator queue and end the conversation.