Published on

September 10, 2009

Understanding Service Broker in SQL Server

Service Broker is a powerful technology introduced in Microsoft SQL Server 2005 that provides messaging and queuing functions between instances. It allows for the sending and receiving of messages, forming a complete channel of communication known as a “conversation”. This functionality is particularly useful when working with single or multiple SQL Server instances.

One of the key advantages of Service Broker is its ability to send messages to remote databases on different servers and process them within a single database. This is achieved through the use of TCP/IP for message exchange between instances. By leveraging this transaction message queuing system, developers can build secure, reliable, and scalable applications.

Service Broker operates based on the concept of independent components called “services”. Applications can send messages to specific services to access their functionality. This loose coupling of applications allows for greater flexibility and independence in message exchange.

The three main components of Service Broker are:

  • Conversation components: These include conversation groups, conversations, and messages.
  • Service definition components: These define the conversations.
  • Networking and security components: These define the infrastructure used for exchanging messages between instances.

Configuring and maintaining Service Broker is relatively easy and is part of routine database administration procedures. Since it is integrated into the Database Engine, it seamlessly integrates with other SQL Server features and provides additional security measures such as preventing unauthorized access from networks and message encryption.

Let’s take a look at a simple script that demonstrates how to configure Service Broker and send/receive messages:

CREATE DATABASE ServiceBrokerTest
GO

USE ServiceBrokerTest
GO

-- Enable Service Broker
ALTER DATABASE ServiceBrokerTest SET ENABLE_BROKER GO

-- Create Message Type
CREATE MESSAGE TYPE SBMessage VALIDATION = NONE
GO

-- Create Contract
CREATE CONTRACT SBContract (SBMessage SENT BY INITIATOR)
GO

-- Create Send Queue
CREATE QUEUE SBSendQueue
GO

-- Create Receive Queue
CREATE QUEUE SBReceiveQueue
GO

-- Create Send Service on Send Queue
CREATE SERVICE SBSendService ON QUEUE SBSendQueue (SBContract)
GO

-- Create Receive Service on Receive Queue
CREATE SERVICE SBReceiveService ON QUEUE SBReceiveQueue (SBContract)
GO

-- Begin Dialog using service on contract
DECLARE @SBDialog uniqueidentifier
DECLARE @Message NVARCHAR(128)

BEGIN DIALOG CONVERSATION @SBDialog FROM SERVICE SBSendService TO SERVICE 'SBReceiveService' ON CONTRACT SBContract WITH ENCRYPTION = OFF

-- Send messages on Dialog
SET @Message = N'Very First Message';
SEND ON CONVERSATION @SBDialog MESSAGE TYPE SBMessage (@Message)

SET @Message = N'Second Message';
SEND ON CONVERSATION @SBDialog MESSAGE TYPE SBMessage (@Message)

SET @Message = N'Third Message';
SEND ON CONVERSATION @SBDialog MESSAGE TYPE SBMessage (@Message)
GO

-- View messages from Receive Queue
SELECT CONVERT(NVARCHAR(MAX), message_body) AS Message FROM SBReceiveQueue
GO

-- Receive messages from Receive Queue
RECEIVE TOP (1) CONVERT(NVARCHAR(MAX), message_body) AS Message FROM SBReceiveQueue
GO

-- Receive messages from Receive Queue
RECEIVE CONVERT(NVARCHAR(MAX), message_body) AS Message FROM SBReceiveQueue
GO

-- Clean Up
USE master
GO

DROP DATABASE ServiceBrokerTest
GO

As you can see, this script demonstrates the step-by-step process of configuring Service Broker and sending/receiving messages. It starts by enabling Service Broker for the database, creating message types, contracts, queues, and services. It then initiates a conversation and sends messages on that conversation. Finally, it demonstrates how to view and receive messages from the receive queue.

Service Broker is a powerful tool that can greatly enhance the functionality and scalability of your SQL Server applications. By leveraging its messaging and queuing functions, you can build secure and reliable applications that can communicate with remote databases on different servers. Its ease of configuration and integration with the Database Engine make it a valuable addition to any SQL Server environment.

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.