Reputation: 160
Is there any option to retrieve the Active Message Count of a queue using the new Azure.messaging.servicebus package?
With the old Microsoft.azure.servicebus you could use ManagementClient that exposes GetQueueRuntimeInfoAsync(String, CancellationToken)
var managementClient = new ManagementClient("queue connection string"));
var runtimeInfo = await managementClient.GetQueueRuntimeInfoAsync("queueName");
var messagesInQueueCount = runtimeInfo.MessageCountDetails.ActiveMessageCount;
Is there a way to achieve something similar? Thank you.
Upvotes: 5
Views: 2953
Reputation: 425
It is amazing how hard it is to find the solution I was looking for since so much is constantly being deprecated and is a moving target. I finally stumbled on this by importing different libraries step by step and stepped through some in debug mode:
from azure.servicebus.management import ServiceBusAdministrationClient
CONNECTION_STR = 'Endpoint=sb://mygroupname.servicebus.windows.net/;SharedAccessKeyName=sbnamespacerule01;SharedAccessKey=bla bla bla Wm3+ASbGYYqQU='
QUEUE_NAME = 'my-queue'
management_client = ServiceBusAdministrationClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
queue_runtime_info = management_client.get_queue_runtime_properties(QUEUE_NAME)
queue_length = queue_runtime_info.total_message_count
print(f"Queue Length: {queue_length}")
# The following attributes are available for queue_runtime_info:
# dead_letter_message_count: 0
# name: 'my-queue'
# scheduled_message_count: 0
# size_in_bytes: 0
# total_message_count: 0
# transfer_dead_letter_message_count: 0
# transfer_message_count: 0
I hope it helps somebody, I spent too much on it to even mention. I should mention that one needs "manage" rights on that queue for this solution.
Upvotes: 1
Reputation: 26057
You can. The starting point would be a similar management client, ServiceBusManagementClient
. It exposes methods to access entity runtime information such as GetQueueRuntimePropertiesAsync()
, which returns QueueRuntimeProperties
. The QueueRuntimeProperties
object has all the info, including ActiveMessageCount
.
Upvotes: 2