Azure Service Bus - PeekBatchAsync returning less

2019-07-31 07:48发布

问题:

In an Azure Service Bus Topic I have a subscription with more than 1000 messages. These messages are consumed by a worker role. For monitoring purposes, I want to peek first 10 messages, to see what kind of messages are waiting to be processed. To accomplish that task I create a subscriptionclient and I call method PeekBatchAsync . The strange behavior is that sometimes PeekBatchAsync returns less than 10 messages (i.e. 2 or 4 messages).

Code Example :

Dim subscriptionClient As SubscriptionClient = messagingFactory.CreateSubscriptionClient(topicName, subscriptionName)
Dim messages = Await subscriptionClient.PeekBatchAsync(10)

More information to clarify the context:

  • I am sure that the subscription contains more than 1000 messages
  • The topic was created with EnablePartitioning = True
  • I'm using library Microsoft.ServiceBus v. 2.3.4.0
  • I'm using sessions
  • The most of the time I got the right number of messages (10) and sometimes I got less messages. I can't figure out what are the exact conditions to replicate the issue

My question is : Why sometimes the method returns less messages than expected?

回答1:

I guess the documentation for the Azure-ServiceBus ReceiveBatch-method also applies in the case of PeekBatchAsync. Basically it says: The max. number of messages (in your case 10) might be returned but it's not guaranteed.

I experienced better results when applying the overload taking two parameters: (int messageCount, TimeSpan serverWaitTime), where one can define a wait-time.