How do you use the SEND statement to send a message in Service Broker?
Posted by JackBrn
Last Updated: August 02, 2024
In SQL Server, Service Broker is a feature for building asynchronous messaging and queuing applications. To send a message using Service Broker, you use the SEND statement. Below is a basic outline of how to use the SEND statement, along with an example.
Prerequisites
Before you can use the SEND statement, ensure that: 1. Service Broker is enabled for your database. 2. You have created the necessary message types, contracts, queues, and services.
Steps to Use the SEND Statement
1. Create Message Type: Define the type of message you want to send.
CREATE MESSAGE TYPE MyMessageType VALIDATION = NONE;
2. Create Contract: Define the contract that specifies which message types can be sent between services.
CREATE CONTRACT MyContract 
   (MyMessageType SENT BY INITIATOR);
3. Create Queues: Create queues where messages will be stored.
CREATE QUEUE MyQueue;
4. Create Services: Create services that will send and receive messages.
CREATE SERVICE MyInitiatorService 
   ON QUEUE MyQueue (MyContract);
5. Send a Message: Use the SEND statement to send a message to the service.
DECLARE @dialog_handle UNIQUEIDENTIFIER;

   -- Start a dialog
   BEGIN Dialog CONVERSATION @dialog_handle
       FROM SERVICE MyInitiatorService
       TO SERVICE 'MyTargetService'
       ON CONTRACT MyContract;

   -- Send the message
   SEND ON CONTRACT MyContract 
   MESSAGE TYPE MyMessageType 
   ('Hello, this is a test message.')
   WITH ENCRYPTION = OFF;
Example Breakdown
1. Starting the Conversation: You initiate a dialog with the BEGIN DIALOG CONVERSATION statement. This creates a conversation handle used for sending messages between services. 2. Sending the Message: The SEND statement sends the message to the target service. You specify the message type and the content of the message. 3. Ending the Conversation: After sending the message, you can end the conversation when appropriate with:
END CONVERSATION @dialog_handle;
This is typically done once the recipient has processed the message.
Complete Example
Here's a complete example that encapsulates the above steps:
-- Step 1: Create Message Type
CREATE MESSAGE TYPE MyMessageType VALIDATION = NONE;

-- Step 2: Create Contract
CREATE CONTRACT MyContract 
(MyMessageType SENT BY INITIATOR);

-- Step 3: Create Queue
CREATE QUEUE MyQueue;

-- Step 4: Create Service
CREATE SERVICE MyInitiatorService
ON QUEUE MyQueue (MyContract);

-- Step 5: Sending a Message
DECLARE @dialog_handle UNIQUEIDENTIFIER;

BEGIN DIALOG CONVERSATION @dialog_handle
FROM SERVICE MyInitiatorService
TO SERVICE 'MyTargetService' -- Substitute with your target service name
ON CONTRACT MyContract;

SEND ON CONTRACT MyContract 
MESSAGE TYPE MyMessageType 
('Hello, this is a test message.');

-- End the conversation when done
END CONVERSATION @dialog_handle;
Conclusion
The SEND statement in Service Broker is an essential part of enabling communication between services in SQL Server. Remember to ensure that your services, contracts, and queues are set up correctly to allow for effective messaging.
Related Content