Is there a way to get the current message count for an Azure topic subscription?
I see that the SubscriptionDescription class has a MessageCount property, but this class appears to only be used to create a subscription. I don't see a way to retrieve a SubscriptionDescription object for an existing subscription.
To peek messages, select Peek Mode in the Service Bus Explorer dropdown. Check the metrics to see if there are Active Messages or Dead-lettered Messages to peek and select either Queue / Subscription or DeadLetter sub-queue. Select the Peek from start button.
Maximum delivery count The default value is 10. Whenever a message has been delivered under a peek-lock, but has been either explicitly abandoned or the lock has expired, the delivery count on the message is incremented. When the delivery count exceeds the limit, the message is moved to the DLQ.
A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account.
The accepted answer is for when using the .NET Framework library with the namespace Microsoft.ServiceBus.Messaging
(nuget package).
For the .NET Standard library with the namespace Microsoft.Azure.ServiceBus
(nuget package) the following code does the trick:
var managementClient = new ManagementClient(connectionString);
var runTimeInfo = await managementClient.GetSubscriptionRuntimeInfoAsync(topicPath, subscriptionName);
var messageCount = runTimeInfo.MessageCountDetails.ActiveMessageCount;
See Microsoft.ServiceBus.Messaging vs Microsoft.Azure.ServiceBus for more details about the differences between the two libraries.
With the retirement of .NET Standard there is a new namespace for .NET 5+ apps, Azure.Messaging.ServiceBus
(nuget package). The code required to do the same with this package is:
var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
var messageCount = runtimeProps.ActiveMessageCount;
Microsoft.Azure.ServiceBus library is deprecated now in favor of Azure.Messaging.ServiceBus. So now this can be achieved with Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient:
var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
var messageCount = runtimeProps.ActiveMessageCount;
I found what I was looking for:
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
long messageCount = subscriptionDesc.MessageCount;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With