Azure Service Bus queue receive options

tivan 1 Reputation point
2024-12-27T11:21:54.01+00:00

Hello,

The Azure Service Bus queues vs. Azure Storage queues comparison page says as pros to SB queues, that long-polling and Push-style API receive modes are also available (while with Storage queues not).

However, I cannot find any documentation on these two options.

Could you please provide references on how to implement polling, long-polling and pushing messages from Azure Service Bus queues?

Thank you!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
655 questions
Azure Queue Storage
Azure Queue Storage
An Azure service that provides messaging queues in the cloud.
111 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 15,006 Reputation points
    2024-12-28T16:51:08.2666667+00:00

    Hello tivan,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to know how you can implement polling, long-polling and pushing messages from Azure Service Bus queues and references.

    1. Polling involves explicitly requesting messages from the queue at regular intervals. This can result in unnecessary requests if no messages are available. Implementation using the ReceiveAsync or ReceiveBatchAsync methods in the Service Bus SDK.
           var client = new QueueClient(connectionString, queueName);
           var message = await client.ReceiveAsync(); // Polling for a single message
      
      Reference - https://zcusa.951200.xyz/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#receive-messages
    2. Long-polling reduces unnecessary polling by allowing the client to wait for a message until a specified timeout occurs. Note that Azure Service Bus does not support indefinite waiting but allows a maximum timeout of 5 minutes. Implementation by using the ReceiveAsync(TimeSpan) method, specifying a longer timeout.
           var client = new QueueClient(connectionString, queueName);
           var message = await client.ReceiveAsync(TimeSpan.FromMinutes(5)); // Waits up to 5 minutes
      
      Key limitation is that Long-polling in Azure Service Bus cannot wait indefinitely. Reference, - https://zcusa.951200.xyz/en-us/azure/service-bus-messaging/message-operations
    3. The push-style API allows the queue to invoke a callback (message handler) whenever a message is available. This is useful for real-time processing and reduces the need for manual polling. Implementation by using RegisterMessageHandler in the SDK.
         var client = new QueueClient(connectionString, queueName);
           client.RegisterMessageHandler(
               async (message, token) =>
               {
                   // Process the message
                   Console.WriteLine($"Received message: {Encoding.UTF8.GetString(message.Body)}");
                   await client.CompleteAsync(message.SystemProperties.LockToken);
               },
               new MessageHandlerOptions(ExceptionHandler) { MaxConcurrentCalls = 5, AutoComplete = true });
      
      Reference, - https://zcusa.951200.xyz/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-queues#receive-messages

    Other reference:

    https://zcusa.951200.xyz/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


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.