1

I have one azure service bus topic subscription where messages keeps pump up.

Below code is basically receive one message at a time and processing it and relevant result stored into database.

I tried to set MaxConcurrentCalls to 10 , but it's exhausted my database connection pool due to the database work design.

So I thought to get 10 messages from subscription at a time (receive in a batch of N number of messages) and want to process with one database call.

I don't see any batch api options, is this possible?

I am using Microsoft.Azure.ServiceBus nuget version 4.1.1.

 _subscriptionClient = new SubscriptionClient(connectionString, topicName, subscriptionName);

            // Register the callback method that will be invoked a message of interest is received
            _subscriptionClient.RegisterMessageHandler(
                async (message, token) =>
                {
                    if (await ProcessMessage(message, token))
                    {
                        await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
                    }
                },
                new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false });
user584018
  • 10,186
  • 15
  • 74
  • 160

1 Answers1

2

There is the concept of prefetching: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements?tabs=net-framework-sdk#prefetching

Prefetching enables the queue or subscription client to load additional messages from the service when it performs a receive operation.

Check the receivebatch here:https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.subscriptionclient.receivebatch?view=azure-dotnet

Example:

SubscriptionClient client = SubscriptionClient.CreateFromConnectionString(connectionString, topic, subName);
client.PrefetchCount = 10;
IEnumerable<BrokeredMessage> messageList = client.ReceiveBatch(5);

Prefetch should be greater than or equal to the number of messages you are expecting to receive from ReceiveBatch.

Prefetch can be up to n/3 times the number of messages processed per second, where n is the default lock duration.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Thanks @Athanasios Kataras. Any code example or tutorial link on this will help me. Could you please share. I don't see something like `_subscriptionClient.ReceiveBatch` – user584018 Sep 11 '20 at 15:42
  • Check examples here: https://stackoverflow.com/questions/31949698/odd-behavior-of-azure-service-bus-receivebatch – Athanasios Kataras Sep 11 '20 at 15:56
  • Thanks. If I do `_subscriptionClient = new SubscriptionClient(connectionString, topicName, subscriptionName);` then I am not getting any method like `_subscriptionClient .ReceiveBatch(100)` . And when I am doing `SubscriptionClient client = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName);` then I am getting error `'SubscriptionClient' does not contain a definition for 'CreateFromConnectionString'`. using both nuget `WindowsAzure.ServiceBus` and `Microsoft.Azure.ServiceBus` – user584018 Sep 11 '20 at 16:11