Published on

September 24, 2007

Introduction to Service Broker in SQL Server

Service Broker is a powerful feature in SQL Server that allows you to build reliable, asynchronous, message-based applications. In this article, we will explore the concepts and benefits of Service Broker and walk through an example of building a simple message-based application.

Why Use Service Broker?

Before we dive into the details of Service Broker, let’s understand why you would want to use it in your applications. Many enterprise applications require asynchronous processing, where requests are queued and processed at a later time. Service Broker provides a robust infrastructure for building such applications, offering features like message ordering, transactions, recoverability, and scalability.

Key Concepts of Service Broker

Service Broker is built on a set of key concepts that form the foundation of message-based applications:

  • Messages: Messages are the fundamental units of communication in Service Broker. They define the content of each message and can be binary, well-formed XML, or valid XML types.
  • Contracts: Contracts define the collection of messages that applications will exchange. They specify the message sequence and the types of messages that can be sent.
  • Services: Services are the endpoints into which messages are posted. They are associated with contracts to validate the messages being sent.
  • Queue: Queues hold the messages and are modeled as tables in SQL Server. Messages are read from queues by service programs, which process the messages.
  • Transport: The transport is the underlying protocol over which messages flow between services. It is a proprietary binary protocol native to SQL Server.
  • Dialog: Services communicate with each other through dialogs. Dialogs ensure in-order processing across transactions and sending/receiving threads.

Building a Service Broker Program

Now that we understand the key concepts, let’s walk through an example of building a simple one-way messaging application using Service Broker. The following steps outline the process:

  1. Identify the required message types and their validation.
  2. Create the contracts that specify the message sequence and types.
  3. Create the queues to hold the messages.
  4. Create the services and bind them to the appropriate queues.
  5. Start passing messages around and reading them.

Here is an example T-SQL script that demonstrates these steps:

-- We will use AdventureWorks as the sample database
USE AdventureWorks

-- Create a message type
CREATE MESSAGE TYPE HelloMessage
VALIDATION = NONE

-- Create a contract
CREATE CONTRACT HelloContract
(HelloMessage SENT BY INITIATOR)

-- Create queues
CREATE QUEUE SenderQueue
CREATE QUEUE ReceiverQueue

-- Create services and bind them to queues
CREATE SERVICE Sender
  ON QUEUE SenderQueue
CREATE SERVICE Receiver
  ON QUEUE ReceiverQueue (HelloContract)

-- Begin conversation and send a message
DECLARE @conversationHandle UNIQUEIDENTIFIER
DECLARE @message NVARCHAR(100)

BEGIN TRANSACTION;
BEGIN DIALOG @conversationHandle
        FROM SERVICE Sender
        TO SERVICE 'Receiver'
        ON CONTRACT HelloContract

SET @message = N'Hello, World';
SEND  ON CONVERSATION @conversationHandle
        MESSAGE TYPE HelloMessage (@message)

COMMIT TRANSACTION

-- Receive a message from the queue
RECEIVE CONVERT(NVARCHAR(max), message_body) AS message
 FROM ReceiverQueue

-- Cleanup
DROP SERVICE Sender
DROP SERVICE Receiver
DROP QUEUE SenderQueue
DROP QUEUE ReceiverQueue
DROP CONTRACT HelloContract
DROP MESSAGE TYPE HelloMessage

Note that the syntax for sending and receiving messages is similar to regular SQL statements. The RECEIVE statement reads a message off the queue and deletes it, while the SELECT statement allows you to peek into the queue without deleting the message.

Conclusion

Service Broker is a powerful feature in SQL Server that enables you to build reliable, asynchronous, message-based applications. By leveraging the capabilities of a database system, Service Broker provides features like message ordering, transactions, recoverability, and scalability. With the concepts and example provided in this article, you can start exploring the possibilities of Service Broker in your own applications.

In future articles, we will dive deeper into Service Broker and explore more advanced programming use cases. Stay tuned!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.