|
1 | | ---- |
2 | | -title: 'Lesson 3: Beginning a Conversation and Transmitting Messages' |
3 | | -description: "In this lesson, you will learn to complete a simple request-reply message cycle in a system configured with an internal activation stored procedure." |
4 | | -ms.prod: sql |
5 | | -ms.technology: configuration |
6 | | -ms.topic: conceptual |
7 | | -author: markingmyname |
8 | | -ms.author: maghan |
9 | | -ms.reviewer: mikeray |
10 | | -ms.date: "03/30/2022" |
11 | | ---- |
12 | | - |
13 | | -# Lesson 3: Beginning a Conversation and Transmitting Messages |
14 | | - |
15 | | -[!INCLUDE [sql-asdbmi](../../includes/applies-to-version/sql-asdbmi.md)] |
16 | | - |
17 | | -In this lesson, you will learn to complete a simple request-reply message cycle in a system configured with an internal activation stored procedure. |
18 | | - |
19 | | -## Procedures |
20 | | - |
21 | | -### Switch to the AdventureWorks2008R2 database |
22 | | - |
23 | | -[!INCLUDE [SQL Server Service Broker AdventureWorks2008R2](../../includes/service-broker-adventureworks-2008-r2.md)] |
24 | | - |
25 | | -- Copy and paste the following code into a Query Editor window. Then, run it to switch context to the **AdventureWorks2008R2** database. |
26 | | -- |
27 | | - |
28 | | - ```sql |
29 | | - USE AdventureWorks2008R2; |
30 | | - GO |
31 | | - ``` |
32 | | - |
33 | | -### Begin a conversation and send a request message |
34 | | - |
35 | | -- Copy and paste the following code into a Query Editor window. Then, run it to start a conversation and send a request message to the **//AWDB/InternalAct/TargetService**. The code must be run in one block because a variable is used to pass a dialog handle from BEGIN DIALOG to the SEND statement. The batch runs the BEGIN DIALOG statement to start the conversation. It builds a request message, and then uses the dialog handle in a SEND statement to send the request message on that conversation. The last SELECT statement displays the text of the message that was sent. |
36 | | - |
37 | | - ```sql |
38 | | - DECLARE @InitDlgHandle UNIQUEIDENTIFIER; |
39 | | - DECLARE @RequestMsg NVARCHAR(100); |
40 | | - |
41 | | - BEGIN TRANSACTION; |
42 | | - |
43 | | - BEGIN DIALOG @InitDlgHandle |
44 | | - FROM SERVICE |
45 | | - [//AWDB/InternalAct/InitiatorService] |
46 | | - TO SERVICE |
47 | | - N'//AWDB/InternalAct/TargetService' |
48 | | - ON CONTRACT |
49 | | - [//AWDB/InternalAct/SampleContract] |
50 | | - WITH |
51 | | - ENCRYPTION = OFF; |
52 | | - |
53 | | - -- Send a message on the conversation |
54 | | - SELECT @RequestMsg = |
55 | | - N'<RequestMsg>Message for Target service.</RequestMsg>'; |
56 | | - |
57 | | - SEND ON CONVERSATION @InitDlgHandle |
58 | | - MESSAGE TYPE |
59 | | - [//AWDB/InternalAct/RequestMessage] |
60 | | - (@RequestMsg); |
61 | | - |
62 | | - -- Diplay sent request. |
63 | | - SELECT @RequestMsg AS SentRequestMsg; |
64 | | - |
65 | | - COMMIT TRANSACTION; |
66 | | - GO |
67 | | - ``` |
68 | | - |
69 | | -### Receive the request and send a reply |
70 | | - |
71 | | -- When you send the request message, Service Broker automatically activates a copy of **TargetActiveProc**. The stored procedure receives the reply message from the **TargetQueueIntAct** and sends a reply message back to the initiator. |
72 | | - |
73 | | -### Receive the reply and end the conversation |
74 | | - |
75 | | -- 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 **InitiatorQueueIntAct**. The END CONVERSATION statement ends the initiator side of the conversation and sends an **EndDialog** message to the target service. 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 previous step. |
76 | | - |
77 | | - ```sql |
78 | | - DECLARE @RecvReplyMsg NVARCHAR(100); |
79 | | - DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER; |
80 | | -
|
81 | | - BEGIN TRANSACTION; |
82 | | -
|
83 | | - WAITFOR |
84 | | - ( RECEIVE TOP(1) |
85 | | - @RecvReplyDlgHandle = conversation_handle, |
86 | | - @RecvReplyMsg = message_body |
87 | | - FROM InitiatorQueueIntAct |
88 | | - ), TIMEOUT 5000; |
89 | | -
|
90 | | - END CONVERSATION @RecvReplyDlgHandle; |
91 | | -
|
92 | | - -- Display recieved request. |
93 | | - SELECT @RecvReplyMsg AS ReceivedReplyMsg; |
94 | | -
|
95 | | - COMMIT TRANSACTION; |
96 | | - GO |
97 | | - ``` |
98 | | - |
99 | | -### End the target side of the conversation |
100 | | - |
101 | | -- When you run the END CONVERSATION statement for the initiator, Service Broker sends an **EndDialog** message to the **TargetQueueIntAct** queue. The **TargetActiveProc** procedure receives the **EndDialog** message and issues an END CONVERSATION that ends the target side of the conversation. |
102 | | - |
103 | | -## Next Steps |
104 | | - |
105 | | -You have successfully completed a request-reply message cycle between the **//AWDB/InternalAct/InitiatorService** and the **//AWDB/InternalAct/TargetService**. You can repeat the steps in this lesson as many times as you want to transmit a request-reply pair of messages. When you have finished investigating the SEND and REPLY statements, you can drop all the objects that were used by the conversation. For more information, see [Lesson 4: Dropping the Conversation Objects](lesson-4-dropping-the-conversation-objects.md). |
106 | | - |
107 | | -## See also |
108 | | - |
109 | | -- [BEGIN DIALOG CONVERSATION (Transact-SQL)](../../t-sql/statements/begin-dialog-conversation-transact-sql.md) |
110 | | -- [SEND (Transact-SQL)](../../t-sql/statements/send-transact-sql.md) |
111 | | -- [RECEIVE (Transact-SQL)](../../t-sql/statements/receive-transact-sql.md) |
112 | | -- [END CONVERSATION (Transact-SQL)](../../t-sql/statements/end-conversation-transact-sql.md) |
113 | | -- [Service Broker Applications](service-broker-applications.md) |
| 1 | +--- |
| 2 | +title: 'Lesson 3: Beginning a Conversation and Transmitting Messages' |
| 3 | +description: "In this lesson, you will learn to complete a simple request-reply message cycle in a system configured with an internal activation stored procedure." |
| 4 | +ms.prod: sql |
| 5 | +ms.technology: configuration |
| 6 | +ms.topic: conceptual |
| 7 | +author: markingmyname |
| 8 | +ms.author: maghan |
| 9 | +ms.reviewer: mikeray |
| 10 | +ms.date: "03/30/2022" |
| 11 | +--- |
| 12 | + |
| 13 | +# Lesson 3: Beginning a Conversation and Transmitting Messages |
| 14 | + |
| 15 | +[!INCLUDE [sql-asdbmi](../../includes/applies-to-version/sql-asdbmi.md)] |
| 16 | + |
| 17 | +In this lesson, you will learn to complete a simple request-reply message cycle in a system configured with an internal activation stored procedure. |
| 18 | + |
| 19 | +## Procedures |
| 20 | + |
| 21 | +### Switch to the AdventureWorks2008R2 database |
| 22 | + |
| 23 | +[!INCLUDE [SQL Server Service Broker AdventureWorks2008R2](../../includes/service-broker-adventureworks-2008-r2.md)] |
| 24 | + |
| 25 | +- Copy and paste the following code into a Query Editor window. Then, run it to switch context to the **AdventureWorks2008R2** database. |
| 26 | + |
| 27 | + ```sql |
| 28 | + USE AdventureWorks2008R2; |
| 29 | + GO |
| 30 | + ``` |
| 31 | + |
| 32 | +### Begin a conversation and send a request message |
| 33 | + |
| 34 | +- Copy and paste the following code into a Query Editor window. Then, run it to start a conversation and send a request message to the **//AWDB/InternalAct/TargetService**. The code must be run in one block because a variable is used to pass a dialog handle from BEGIN DIALOG to the SEND statement. The batch runs the BEGIN DIALOG statement to start the conversation. It builds a request message, and then uses the dialog handle in a SEND statement to send the request message on that conversation. The last SELECT statement displays the text of the message that was sent. |
| 35 | + |
| 36 | + ```sql |
| 37 | + DECLARE @InitDlgHandle UNIQUEIDENTIFIER; |
| 38 | + DECLARE @RequestMsg NVARCHAR(100); |
| 39 | +
|
| 40 | + BEGIN TRANSACTION; |
| 41 | +
|
| 42 | + BEGIN DIALOG @InitDlgHandle |
| 43 | + FROM SERVICE |
| 44 | + [//AWDB/InternalAct/InitiatorService] |
| 45 | + TO SERVICE |
| 46 | + N'//AWDB/InternalAct/TargetService' |
| 47 | + ON CONTRACT |
| 48 | + [//AWDB/InternalAct/SampleContract] |
| 49 | + WITH |
| 50 | + ENCRYPTION = OFF; |
| 51 | +
|
| 52 | + -- Send a message on the conversation |
| 53 | + SELECT @RequestMsg = |
| 54 | + N'<RequestMsg>Message for Target service.</RequestMsg>'; |
| 55 | +
|
| 56 | + SEND ON CONVERSATION @InitDlgHandle |
| 57 | + MESSAGE TYPE |
| 58 | + [//AWDB/InternalAct/RequestMessage] |
| 59 | + (@RequestMsg); |
| 60 | +
|
| 61 | + -- Diplay sent request. |
| 62 | + SELECT @RequestMsg AS SentRequestMsg; |
| 63 | +
|
| 64 | + COMMIT TRANSACTION; |
| 65 | + GO |
| 66 | + ``` |
| 67 | + |
| 68 | +### Receive the request and send a reply |
| 69 | + |
| 70 | +- When you send the request message, Service Broker automatically activates a copy of **TargetActiveProc**. The stored procedure receives the reply message from the **TargetQueueIntAct** and sends a reply message back to the initiator. |
| 71 | + |
| 72 | +### Receive the reply and end the conversation |
| 73 | + |
| 74 | +- 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 **InitiatorQueueIntAct**. The END CONVERSATION statement ends the initiator side of the conversation and sends an **EndDialog** message to the target service. 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 previous step. |
| 75 | + |
| 76 | + ```sql |
| 77 | + DECLARE @RecvReplyMsg NVARCHAR(100); |
| 78 | + DECLARE @RecvReplyDlgHandle UNIQUEIDENTIFIER; |
| 79 | +
|
| 80 | + BEGIN TRANSACTION; |
| 81 | +
|
| 82 | + WAITFOR |
| 83 | + ( RECEIVE TOP(1) |
| 84 | + @RecvReplyDlgHandle = conversation_handle, |
| 85 | + @RecvReplyMsg = message_body |
| 86 | + FROM InitiatorQueueIntAct |
| 87 | + ), TIMEOUT 5000; |
| 88 | +
|
| 89 | + END CONVERSATION @RecvReplyDlgHandle; |
| 90 | +
|
| 91 | + -- Display recieved request. |
| 92 | + SELECT @RecvReplyMsg AS ReceivedReplyMsg; |
| 93 | +
|
| 94 | + COMMIT TRANSACTION; |
| 95 | + GO |
| 96 | + ``` |
| 97 | + |
| 98 | +### End the target side of the conversation |
| 99 | + |
| 100 | +- When you run the END CONVERSATION statement for the initiator, Service Broker sends an **EndDialog** message to the **TargetQueueIntAct** queue. The **TargetActiveProc** procedure receives the **EndDialog** message and issues an END CONVERSATION that ends the target side of the conversation. |
| 101 | + |
| 102 | +## Next Steps |
| 103 | + |
| 104 | +You have successfully completed a request-reply message cycle between the **//AWDB/InternalAct/InitiatorService** and the **//AWDB/InternalAct/TargetService**. You can repeat the steps in this lesson as many times as you want to transmit a request-reply pair of messages. When you have finished investigating the SEND and REPLY statements, you can drop all the objects that were used by the conversation. For more information, see [Lesson 4: Dropping the Conversation Objects](lesson-4-dropping-the-conversation-objects.md). |
| 105 | + |
| 106 | +## See also |
| 107 | + |
| 108 | +- [BEGIN DIALOG CONVERSATION (Transact-SQL)](../../t-sql/statements/begin-dialog-conversation-transact-sql.md) |
| 109 | +- [SEND (Transact-SQL)](../../t-sql/statements/send-transact-sql.md) |
| 110 | +- [RECEIVE (Transact-SQL)](../../t-sql/statements/receive-transact-sql.md) |
| 111 | +- [END CONVERSATION (Transact-SQL)](../../t-sql/statements/end-conversation-transact-sql.md) |
| 112 | +- [Service Broker Applications](service-broker-applications.md) |
0 commit comments