Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

651


Chapter 25: Asynchronous Messaging with Service Broker


25


WHERE name='HelloWorldMessage';
GO

--Query the sys.service_contracts catalog view
SELECT * FROM sys.service_contracts
WHERE name='HelloWorldContract'
GO

--Query the sys.service_queues_type catalog view
SELECT * FROM sys.service_queues
WHERE name in ('TargetQueue','InitiatorQueue');
GO

--Query the sys.services catalog view
SELECT * FROM sys.services
WHERE name in ('InitiatorService','TargetService');
GO

For a complete description of all Catalog Views available for Service Broker visit http://
msdn.microsoft.com/en-us/library/ms173780.aspx

Working with Dialogs


With the Service Broker infrastructure created, messages can be sent between services and
received from the queue. Messages exist as part of a conversation, and multiple conversa-
tions can be contained in a conversation group.

Service Broker makes multiple queue readers possible by locking the conversation group;
however, locking the conversation group with normal database commands is almost impos-
sible to accomplish effi ciently. Service Broker accordingly uses a new kind of database lock,
and only Service Broker commands understand this lock type.

Sending a Message to the Queue
The following code creates a conversation that is identifi ed by a conversationhandle
GUID. SEND places a single message onto a queue within a transaction. The BEGIN
CONVERSATION command opens the conversation, and the SEND command actually places
the message into the queue:

BEGIN TRANSACTION ;

DECLARE @message XML ;
SET @message = N'<message>Hello, World!</message>' ;

DECLARE @conversationHandle UNIQUEIDENTIFIER ;

BEGIN DIALOG CONVERSATION @conversationHandle

c25.indd 651c25.indd 651 7/31/2012 9:21:38 AM7/31/2012 9:21:38 AM


http://www.it-ebooks.info
Free download pdf