Search code examples
sql-serverservice-broker

Service Broker - How to response the message to different queue


I'm new to Service Broker

Message Type

CREATE MESSAGE TYPE [http://ssb.csharp.at/SSB_Book/c03/RequestMessage]
VALIDATION = WELL_FORMED_XML
GO

CREATE MESSAGE TYPE [http://ssb.csharp.at/SSB_Book/c03/ResponseMessage]
    VALIDATION = WELL_FORMED_XML
GO

Contract

CREATE CONTRACT [http://ssb.csharp.at/SSB_Book/c03/HelloWorldContract]
(
    [http://ssb.csharp.at/SSB_Book/c03/RequestMessage] SENT BY INITIATOR,
    [http://ssb.csharp.at/SSB_Book/c03/ResponseMessage] SENT BY TARGET
)
GO

Queue

CREATE QUEUE InitiatorQueue
    WITH ACTIVATION
    (
        STATUS = ON,
        PROCEDURE_NAME = [ProcessResponseMessage],
        MAX_QUEUE_READERS = 1,
        EXECUTE AS SELF
    )

CREATE QUEUE TargetQueue
    WITH ACTIVATION
    (
        STATUS = ON,
        PROCEDURE_NAME = [ProcessRequestMessage],
        MAX_QUEUE_READERS = 1,
        EXECUTE AS SELF
    )

Service

CREATE SERVICE InitiatorService
ON QUEUE InitiatorQueue
(
    [http://ssb.csharp.at/SSB_Book/c03/HelloWorldContract]
)
GO

CREATE SERVICE TargetService
ON QUEUE TargetQueue
(
    [http://ssb.csharp.at/SSB_Book/c03/HelloWorldContract]
)
GO

Codes above are the example.

The question is I want to response the message to another queue(ErrorQueue for example) instead of InitiatorQueue. I've did research on Google but I can't find any resources that related to my question.

I've some idea but don't know if it's working:

  1. Add another message type into Contract:
    [http://ssb.csharp.at/SSB_Book/c03/ErrorMessage] SENT BY INITIATOR
    But I don't know whether to set it as INITIATOR, TARGET or ANY
  2. Create another Contract for it, if this is the solution please provide an example.

Thanks


Solution

  • The thing processing messages on your queue can make the choice as to what to do. If you want to send to a different queue, you'll need to send on a different conversation though because sending on the same conversation as the one the original message came in on will send it back to the place that sent the message. You'll need another message type, contract, queue, and service to send that error message to. Let me know if that's not enough to get you going and I can elaborate a little further.