Lesson 6: Receiving the Reply and Ending the Conversation
New: 15 September 2007
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; RECEIVE TOP(1) @RecvReplyDlgHandle = conversation_handle, @RecvReplyMsg = message_body FROM InstInitiatorQueue; END CONVERSATION @RecvReplyDlgHandle; -- Display recieved request. SELECT @RecvReplyMsg AS ReceivedReplyMsg; COMMIT TRANSACTION; GO
Next Steps
This concludes the tutorial. Tutorials are brief overviews and do not describe all available options. Tutorials have simplified logic and error handling to better focus on fundamental operations. To create efficient, reliable, and robust conversations, you need more complex code than the example in this tutorial.
Return To The Service Broker Tutorials
See Also
Other Resources
RECEIVE (Transact-SQL)
END CONVERSATION (Transact-SQL)
WAITFOR (Transact-SQL)
Service Broker Programming Basics