Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the message count for Azure Topic Subscription?

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.

like image 430
egelvin Avatar asked Dec 19 '12 16:12

egelvin


People also ask

How do I check messages on Azure Service Bus queue?

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.

What is delivery count in Azure Service Bus?

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.

What is the maximum size of a message in the Azure message queue?

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.


3 Answers

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;
like image 169
Simon Ness Avatar answered Nov 04 '22 17:11

Simon Ness


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;
like image 39
AndrewG Avatar answered Nov 04 '22 16:11

AndrewG


I found what I was looking for:

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
long messageCount = subscriptionDesc.MessageCount;
like image 39
egelvin Avatar answered Nov 04 '22 15:11

egelvin