-- Create the database to be used
Create Database helloworlddb
Go
Use helloworlddb
Go
-- Create two types of messages to be used. The message we want to use will be
-- String instead of XML-So verification is not required
Create Message Type [helloworldrequest] Validation = none
Create Message Type [helloworldresponse] Validation = none
-- Create a message type restriction dialog box
-- Specification. The request is sent by the initialization program of the dialog box.
-- The response message is sent by the target of the dialog box.
Create contract [helloworldcontract]
(
[Helloworldrequest] sent by initiator,
[Helloworldresponse] sent by Target
)
-- Create two queues in which the dialog box communicates. A
-- The dialog box requests two queues.
Create queue [helloworldtargetqueue]
Create queue [helloworldinitiatorqueue]
-- Create a service for the endpoint of the naming dialog box. The Service will
-- The session endpoint is connected to the queue.
Create service [helloworldrequestservice] on queue [helloworldtargetqueue]
(
[Helloworldcontract]
)
Create service [helloworldresponseservice] on queue [helloworldinitiatorqueue]
Go
Use helloworlddb
Go
Set nocount on
Declare @ conversationhandle uniqueidentifier
Begin transaction
-- Start the hello World Service conversation
Begin dialog @ conversationhandle
From Service [helloworldresponseservice]
To service 'helloworldrequestservice'
On contract [helloworldcontract]
With encryption = OFF, lifetime = 600;
-- Send a message
Send on conversation @ conversationhandle
Message Type [helloworldrequest] (N 'Hello World ')
Commit
Use helloworlddb
Go
-- Check the target queue to confirm that the message has been delivered
Select * from [helloworldtargetqueue]
Go
-- Convert the message body to a string so that we can view the content contained in it.
Select cast (message_body as nvarchar (max) from [helloworldtargetqueue]
Go
-- Use the receive command to receive messages from the queue
-- Declare variables to store received data
Set nocount on
Declare @ conversationhandle uniqueidentifier
Declare @ message_body nvarchar (max)
Declare @ message_type_name sysname;
-- The Service Broker command is always in the transaction
Begin transaction;
The format of the -- receive command is similar to a selection list. First List
-- The column to be retrieved, and specify the message to be retrieved from it
-- Queue
Receive top (1) -- receives only one message, so we can directly save it to the variable.
@ Message_type_name = message_type_name,
-- Received message type
@ Conversationhandle = conversation_handle,
-- Dialog identifier
-- The following statement is used to receive the message.
@ Message_body = message_body
--
-- Varbinary (max) blob message content
From [helloworldtargetqueue]
Print @ message_body
-- If this is a hello World message, use the corresponding greeting.
If @ message_type_name = n' helloworldrequest'
Begin
Send on conversation @ conversationhandle
-- Use the same session of the following message receiving statement
Message Type [helloworldresponse]
(N 'Hello from' + @ servername)
-- This is the only message we want to receive from the initialization program, so
-- Now you can safely end the conversation.
End conversation @ conversationhandle
End
-- Submit a transaction
-- If we roll back at this time, all content will be returned
-- Status at the beginning-the message is returned to the queue and no response is sent.
Commit
Go
-- Confirm that we have deleted the message from the queue
Select cast (message_body as nvarchar (max) from [helloworldtargetqueue]
Go
Use helloworlddb
Go
Select cast (message_body as nvarchar (max) from [helloworldinitiatorqueue]
Go
Receive
Cast (message_body as nvarchar (max ))
From [helloworldinitiatorqueue]