http://www.developer.com/db/article.php/3640771/Getting-Started-with-SQL-Server-Service-Broker.htm
Service Broker is a new feature in SQL Server 2005 that brings queuing and reliable messaging to SQL Server. Service Broker provides the plumbing to let you pass messages between applications, using SQL Server as the transport mechanism. Applications can use a single shared SQL Server database for this purpose or distribute their work across multiple databases. Service Broker handles all of the details of message passing, including: In this article I'll introduce you to the basics of Service Broker, including the terminology that it uses and the SQL statements that you'll need to implement a Service Broker application. This is just a quick survey; for details, refer to SQL Server Books Online. Service Broker introduces a number of new terms to the SQL Server lexicon. These include: The basic steps involved in creating any Service Broker application include: Each of these tasks has a corresponding T-SQL extension. To define a message type for a Service Broker application, you use the After defining messages, you can use the Every service managed by Service Broker requires queues to hold messages sent and received by that service. You can create these with the Now that the queues exist, you can use Now that all the pieces are in place, you can test sending and receiving messages between the two services. To do this, youll use three T-SQL statements: To receive messages, you can use the RECEIVE statement: Now you've seen how simple it is to set up queues and send messages using Service Broker, but you can go much further with it. In many applications, youll want to process incoming messages automatically. For example, you might want to take those inventory messages and automatically change rows in an inventory table. You can automate the response to an incoming message by taking advantage of the ability to associate an activation stored procedure with a queue in the CREATE QUEUE statement. You can also use Service Broker to build distributed applications, where the sending and receiving queues are in different databases, even on different physical machines. If you do this, you'll want to be familiar with the In older versions of SQL Server, developing asynchronous, reliable, message-based applications was difficult or impossible. With Service Broker, Microsoft has given you all the infrastructure you need to make the basics easy - so you can concentrate on your business needs. Keep this tool in mind and you're bound to find a use for it. Mike Gunderloy is the Senior Technology Partner for Adaptive Strategy, a
Washington State consulting firm. You can read more of
Mike's work at his Larkware Web site, or
contact him at MikeG1@larkfarm.com.
Getting Started with SQL Server Service Broker
October 30, 2006
Service Broker Terminology
Creating a Service Broker Application
Defining Message Types
CREATE MESSAGE TYPE statement. As part of this statement, you can specify whether a message must conform to a particular XML schema or be otherwise validated.
CREATE MESSAGE TYPE StockMessage
VALIDATION = NONE
Defining Contracts
CREATE CONTRACT statement to define a contract:
CREATE CONTRACT StockContract
(StockMessage SENT BY INITIATOR)
Creating Queues
CREATE QUEUE statement:
CREATE QUEUE StockSendQueue
CREATE QUEUE StockReceiveQueue
Creating Services
CREATE SERVICE to build services to use them:
CREATE SERVICE StockSendService
ON QUEUE StockSendQueue (StockContract)
CREATE SERVICE StockReceiveService
ON QUEUE StockReceiveQueue (StockContract)
Sending and Receiving Messages
BEGIN DIALOG CONVERSATION sets up the conversation between the two services.SEND sends a message.RECEIVE receives a message.Sending Messages
To start a conversation between two services with a common contract, use the BEGIN DIALOG CONVERSATION statement, which will return a unique dialog handle. After youve created the dialog and stored the dialog handle, youre ready to send messages. For this, you use the SEND statement. Here's how it looks when you put the pieces together:
DECLARE @StockDialog uniqueidentifier
DECLARE @Message nvarchar(128)
BEGIN DIALOG CONVERSATION @StockDialog
FROM SERVICE StockSendService
TO SERVICE 'StockReceiveService'
ON CONTRACT StockContract
WITH ENCRYPTION = OFF
SET @Message = N'Add 12 widgets to inventory';
SEND ON CONVERSATION @StockDialog
MESSAGE TYPE StockMessage (@Message)
SET @Message = N'Remove 4 springs from inventory';
SEND ON CONVERSATION @StockDialog
MESSAGE TYPE StockMessage (@Message)
SET @Message = N'Add 7 twonkies to inventory';
SEND ON CONVERSATION @StockDialog
MESSAGE TYPE StockMessage (@Message)
Receiving Messages
RECEIVE CONVERT(NVARCHAR(max), message_body) AS message
FROM StockReceiveQueue
What's Next?
CREATE ROUTE statement, which tells Service Broker how to find services on other computers. With this statement, you can store and forward queues in practically any architecture, as long as a TCP/IP route exists between the databases.About the Author