aceinthehole
aceinthehole

Reputation: 5232

Determining how many messages are on the Azure Service Bus Queue

I know there is a way to determine the number of messages (or approximate number) in the Azure Queue (Store Account); however is there a way to query for the number of pending messages on an Azure Service Bus queue?

Upvotes: 37

Views: 32873

Answers (10)

Daniel Revell
Daniel Revell

Reputation: 8586

Correct answer as of 2020+

Use of new packages as follows:

<PackageReference Include="Azure.Messaging.ServiceBus" Version="x.x.x" />

also two namespaces in the same package

using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;

and then you can use the new class ServiceBusAdministrationClient

var administrationClient = new ServiceBusAdministrationClient("connectionString");
var props = await administrationClient.GetQueueRuntimePropertiesAsync("queue");
var messageCount = props.Value.ActiveMessageCount;

Upvotes: 19

Shubham Gupta
Shubham Gupta

Reputation: 81

As per the recommendation by Microsoft, it is recommended to use Microsoft.Azure.ServiceBus in which you can easily fetch the message count by

var managementClient = new ManagementClient("connection string for queue");
var queue = await managementClient.GetQueueRuntimeInfoAsync("queue name");
var messages = queue.MessageCount;

Upvotes: 0

f1xxxer
f1xxxer

Reputation: 1

I've spent good 2 hours digging through docs to get that and for people using .net core and Microsoft.Azure.ServiceBus nuget package, code looks like that:

var managementClient = new ManagementClient("queue connection string"));
var runtimeInfo = await managementClient.GetQueueRuntimeInfoAsync("queueName");

var messagesInQueueCount = runtimeInfo.MessageCountDetails.ActiveMessageCount;

Aparently you get the information about all Counts(including deadletter, active, etc.) from QueueRuntimeInfo object instead of old QueueDescription object.

Upvotes: 0

indofraiser
indofraiser

Reputation: 1024

Based off what Joseph had as an answer I came up with, but for Topics and Subscriptions.

public async Task<long> GetCounterMessages()
        {
            var client = new ManagementClient(ServiceBusConnectionString);    
            var subs = await client.GetSubscriptionRuntimeInfoAsync(TopicName, SubscriptionName);
            var countForThisSubscription = subs.MessageCount;  //// (Comes back as a Long.)               
            return countForThisSubscription;
        }

Upvotes: 1

golfalot
golfalot

Reputation: 1292

Here's a PowerShell example to continually eyeball the queue length as used in Azure Portal Cloud Shell

cd "Azure:\<MySubscription>\"
while (1) {(Get-AzureRmServiceBusQueue -ResourceGroup <myRG> -NamespaceName <myNS> -QueueName <myQueueName>).CountDetails | Select -expand ActiveMessageCount}

Upvotes: 0

Justin Baird
Justin Baird

Reputation: 19

I ran into this same problem trying to get the count from the dead letter queue. It looks like the deadletterqueue doesn't allow you to get a count directly, you get it from the MessageCountDetails of the normal Queue.

string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.Connstr"].ToString();
NamespaceManager nsmgr = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(connectionString);
return nsmgr.GetQueue(QueueName).MessageCountDetails.DeadLetterMessageCount;

Upvotes: 0

Katsifaris
Katsifaris

Reputation: 374

It is called MessagesCountDetails.ActiveMessageCount. It returns the number of the Active Messages in the Queue. You probably have some Dead letter messages:

var msg = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(Settings.Default.ConnectionString);
numofmessages.Text = msg.GetQueue(QueueName).MessageCountDetails.ActiveMessageCount.ToString();

Upvotes: 12

Joseph
Joseph

Reputation: 361

var nsmgr = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(connectionString);
long count = nsmgr.GetQueue(queueName).MessageCount;

Upvotes: 33

saransh77
saransh77

Reputation: 59

Also..you can check the pending messages on Azure Management Portal...on the dashboard for service bus queue...under quick glance...you can see the queue length...this is the number of current/pending messages in length at the time of dashboard page load.

Upvotes: -3

David Makogon
David Makogon

Reputation: 71121

have you looked at the Queue Description API? There's a property called MessageCount.

Here's the .NET SDK reference documentation page as well.

Upvotes: 7

Related Questions