Getting count of Azure service bus using python

Viewed 322

I want to know there are too many service bus messages in my queue and if need be replay(dead-lettered messages) them. I am currently able to peek and replay the messages but I have no clue on how to get the active/dead-lettered-message count. I'm basically looking for the python equivalent of this powershell command.

az servicebus queue show --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --query countDetails

There's documentation for this on powershell but I wasn't able to find anything for python. Can someone. Can someone kindly help me with this?

2 Answers

You can use Azure Service Bus Python SDK.

First get the properties of a queue using get_queue method which will contain property called message_count which will give you the count of messages.

queue = bus_service.get_queue('queue1')
print queue.message_count

Can't add as a comment, other answer is correct wrt Azure Service Bus Python SDK however code is slightly different

Sample code:

with azure.servicebus.management.ServiceBusAdministrationClient.from_connection_string(CONNECTION_STR) as client:
 print(client.get_queue_runtime_properties(QUEUE_NAME).active_message_count)
  

Key part is using the get_queue_runtime_properties and not just get_queue

(May be the case that the SDK has updated since)

Hope this helps someone!

Related