Service Broker in 10 steps for beginners

Posted: January 17, 2007 in Uncategorized

Initialy my thought was should i post this or not ,since a lot alreday happen and written on this but fianlly i found that i can post this may be this ll be useful for someone else.This is what i had used and saved as a script during my test of the service broker functionality.So here you go:-

steps to create a simple service broker what you need

  • –message
  • –contract
  • –queue
  • –service
  • –route

–1.First you need to Alter Database to enable ServiceBroker if it is not yet enabled

–Test is my DBname

ALTER DATABASE Test SET ENABLE_BROKER

Go

–2. Create a message type. In addition you can supply the schema to validate the Messagetype

–the other schema validation options are EMPTY,WELL_FORMED_XML

CREATE MESSAGE TYPE MyTestMessageType

Validation=NONE

Go

–3.Now lets create a contract on the above message type

–you can specify the ownername and initiaitor,target or any for the sentby

–we ll use ANY means message can be send by both initiator and target

CREATE CONTRACT MYTestContract

(MyTestMessageType SENT BY ANY)

Go

–4.We have the messagetype and contract now let Create a send and receive queue

CREATE QUEUE MyTestSendQueue

CREATE QUEUE MyTestReceiveQueue

Go

–5.Now we need the Service which we ll use to send and receive messages

–you can create service with one,multiple or no contract

–what we need here is the Queue and the contract

CREATE SERVICE MyTestSendService

ON QUEUE MyTestSendQueue (MYTestContract)

CREATE SERVICE MyTestReceiveService

ON QUEUE MyTestReceiveQueue (MYTestContract)

Go

–6. Now lets begin the dialog from one service to another

— we need the from and to services and the contract

–optionally we can specify the lifetime , encryption and conversion group

DECLARE @dialog_handle UNIQUEIDENTIFIER

BEGIN DIALOG CONVERSATION @dialog_handle

FROM SERVICE MyTestSendService

TO SERVICE ‘MyTestReceiveService’

ON CONTRACT MYTestContract

WITH ENCRYPTION=OFF

 

–OK WE have setup Now Let Start Sending and Receiving messages

–7.what we need here is first get the conversion handle from tr

— The first step is to get the conversation handle from the sys.conversion_endpoints

DECLARE @conversation_handle uniqueidentifier

SELECT @conversation_handle = conversation_handle

FROM sys.conversation_endpoints

WHERE far_service = ‘MyTestReceiveService’;

–8.send conversion using the handle

SEND ON CONVERSATION @conversation_handle

MESSAGE TYPE MyTestMessageType

(N’Test Message Send by Sender:Example Shreeman ‘)

 

— 9.Now receive from the queue

RECEIVE convert(nvarchar(max), message_body)

FROM MyTestReceiveQueue

 

–10.Drop all see the order and the case sensitiveness

–do also remember the quotes used in to Service in section 6

— and the farservice one

–finally the drop order would be

–drop service MyTestSendService

–drop service MyTestReceiveService

–drop contract MYTestContract

–drop queue MyTestSendQueue

–drop queue MyTestReceiveQueue

–drop message type MyTestMessageType

–ALTER DATABASE Test SET DISABLE_BROKER

 Here are few articles which might give you further start:-

roger’s archotecting service broker:http://msdn2.microsoft.com/en-us/library/aa964144.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s