Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
RECEIVEstatement retrieves the request message, then the followingSELECTstatement displays the text so that you can verify that it's the same message that was sent in the previous step. TheIFstatement tests whether the received message is a request message type, and if aSENDstatement is used to send a reply message back to the initiator. TheEND CONVERSATIONstatement is used to end the target side of the conversation. The finalSELECTstatement 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
Related content
- END CONVERSATION (Transact-SQL)
- RECEIVE (Transact-SQL)
- SEND (Transact-SQL)
- WAITFOR (Transact-SQL)
- Service Broker applications
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.