Monday, March 26, 2012

how to run the Service Broker Samples

I am trying to run the Readme_HelloWorld sample on the SQL server 2005 Books online. But don't know where I can get the setup.sql, SendMessage.sql, ReceiveMessage.sql scripts.

I followed two examples on the blog, but they did not work at all. Here are the scripts:

/* example 1 */
create database TestSB
go
use TestSB
go
create queue TxQ
create queue RxQ
create message type Msg
create contract MsgContract(Msg SENT BY ANY)
create service TxSve on Queue TxQ
Create service RxSve on Queue RxQ
declare @.h uniqueidentifier
begin dialog conversation @.h
from service TxSve to Service 'RxSve'
on contract MsgContract;
send on conversation @.h Message type Msg ('<hello>World</hello>')
end conversation @.h with CLEANUP;

select *
from RxQ

/* example 2 */
use testsb
go

create message type HelloMessage
validation = NONE
go

/** 2. create contract **/
create contract HelloContract
(HelloMessage SENT by initiator)

/** 3. create queue **/
create queue SenderQueue
create queue ReceiverQueue

/** 4. create service **/
create service sender
on queue SenderQueue

create service receiver
on queue ReceiverQueue(HelloContract)

declare @.handle uniqueidentifier
declare @.message nvarchar(100)

begin
begin transaction;
begin dialog conversation @.handle
from service sender
to service 'receiver'
on contract HelloContract
set @.message = N'Hello, World';
send on conversation @.handle
message type HelloMessage(@.message)
end conversation @.handle
commit transaction
end
go

Receive convert(nvarchar(max), message_body) as message1
from ReceiverQueue

select convert(nvarchar(max), message_body) as message1
from ReceiverQueue

select *
from SenderQueue

select *
from Receiverqueue

Any assistance or WORKING samples are greatly appreciated.

create database TestSB

go

use TestSB

go

create queue TxQ

create queue RxQ

create message type Msg

create contract MsgContract(Msg SENT BY ANY)

create service TxSve on Queue TxQ

Create service RxSve on Queue RxQ ([MsgContract]);

go

declare @.h uniqueidentifier

begin dialog conversation @.h

from service TxSve to Service 'RxSve'

on contract MsgContract

with encryption = off;

send on conversation @.h Message type Msg ('<hello>World</hello>');

end conversation @.h;

receive cast(message_body as XML), * from RxQ;

The sample has a number of problems:

- the target service must be bound to the contract used, the fix is to add the ([MsgContract]) clause to the second CREATE SERVICE.

- dialogs with encryption require a database master key. Encryption is not needed within a SQL instance, fix is to add the ENCRYPTION = OFF to the BEGIN DIALOG statement.

- one should never use END CONVERSATION ... WITH CLEANUP, see http://blogs.msdn.com/remusrusanu/archive/2006/01/27/518455.aspx

- This one I didn't fix, but one should not use the fire-and-forget message pattern, see http://blogs.msdn.com/remusrusanu/archive/2006/04/06/570578.aspx

HTH,
~ Remus

|||Thank you very much, Remus. It works.

No comments:

Post a Comment