Question

Let say I have two instances of the same app interacting with a backend service in Service Broker. How can each instance know to handle only conversations it initiated and ignore the rest? If I recall correctly, every RECEIVE will remove the message from the queue.

Here's an example:

-- Assume the SquareService return the square of the number sent to it

-- Instance 1
BEGIN DIALOG @Conversation1
    FROM SERVICE InitService
    TO SERVICE 'SquareService'
    ON CONTRACT (MyContract)
    WITH ENCRYPTION = OFF;

    SEND ON CONVERSATION @Conversation1 MESSAGE TYPE MyMessageType('1');

-- Instance 2
BEGIN DIALOG @Conversation2
    ...;
    SEND ON CONVERSATION @Conversation2 MESSAGE TYPE MyMessageType('2');

Now who should I write the RECEIVE statement so that Instance 1 will correctly get 1 and Instance 2 get 4 back?

Was it helpful?

Solution

You are already using a Conversation Group. Is this not sufficient for your needs when Receiving the messages? -> using GET CONVERSATION GROUP and RECEIVE together you can read more about it here: http://technet.microsoft.com/en-us/library/ms166131%28v=sql.105%29.aspx and also here Sql Server Service Broker Conversation Groups

OTHER TIPS

I'm assuming you have an InitQueue associated with your InitService. You can use a WHERE clause with RECEIVE to listen for messages on the same conversation:

WAITFOR (RECEIVE @response = CONVERT(xml, message_body)
    FROM InitQueue -- InitService setup to use InitQueue?
    WHERE conversation_handle = @Conversation1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top