Receive messages from an Azure Service Bus queue with a specific correlation id

srisa 21 Reputation points
2021-11-24T21:05:11.033+00:00

Is there any way of only receive messages from an Azure Service Bus queue with a specific correlation id?

The scenario is:

  1. I am receiving a REST API call to my application A
  2. My application A will generate an unique correlation id
  3. My application A will send a request to another application B and include the correlation id. This request is not sent on the Azure Service Bus - with via another mechanism.
  4. The application B will perform some logic and send a reply message on an Azure Service Bus queue with the correlation id it received in (3)
  5. My application A wants to read only the message from the queue with the corresponding correlation id and generate a response to the initial caller based on this reply message.

I see that there is a concept of sessions, but it seems a bit overkill to create new sessions for each request to my application. I cannot reuse correlation id, because I need them to be unique for each request to my application. I see that an alternative could be to use a topic and subscribe with a filter, but that also seems like overkill.

Any ideas?

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2021-11-26T09:12:34.783+00:00

    @srisa Please confirm my understanding it looks like only application B is sending a message to the service bus and application A will be consuming that processed message once your business logic is completed.

    REST Call --> Application A (adds unique correlation id and calls B) --> Application B (forwards the same unique correlation id) --> B application (send message to service bus) --> Application A consumes only that message

    If my understanding is correct then when your Application B is sending the message to the service bus you can send your unique correlation id as the message ID of message while sending the message to the service bus. Now when your Application A receives the REST call you already have the unique correlation id and now you will be waiting for other systems to process the request.

    Updated:
    Once you have forwarded the request to system B now your application A needs to wait (do-while) to check the message with that sequence number using Receive async method. At system B while sending the message set the message ID as a unique correlation id and message state as deferral along with that there will be overhead you to maintain the sequence number with the message ID. The better option would be using sessions for an individual request for your scenario.

    153128-image.png

    To learn more about message defer please refer to this document.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.