[英]How to check the Azure Service Bus queue is empty?
I'm using azure service bus queues in my application and here my question is, is there a way how to check the message queue is empty so that I can shutdown my containers and vms to save cost.我在我的应用程序中使用 azure 服务总线队列,这里我的问题是,有没有办法检查消息队列是否为空,以便我可以关闭容器和 vms 以节省成本。 If there is way to get that please let me know, preferably in python.
如果有办法让我知道,最好是在 python 中。
Thanks谢谢
For this, you can use Azure Service Bus Python SDK
.为此,您可以使用
Azure Service Bus Python SDK
。 What you would need to do is get the properties of a queue using get_queue
method that will return an object of type Queue
.您需要做的是使用
get_queue
方法获取队列的属性,该方法将返回Queue
类型的对象。 This object exposes the total number of messages through message_count
property.此对象通过
message_count
属性公开消息总数。 Please note that this count will include count for active messages, dead-letter queue messages and more.请注意,此计数将包括活动消息、死信队列消息等的计数。
Here's a sample code to do so:这是一个示例代码:
from azure.servicebus import ServiceBusService, Message, Queue
bus_service = ServiceBusService(
service_namespace='namespacename',
shared_access_key_name='RootManageSharedAccessKey',
shared_access_key_value='accesskey')
queue = bus_service.get_queue('taskqueue1')
print queue.message_count
Source code for Azure Service Bus SDK for Python is available on Github: https://github.com/Azure/azure-sdk-for-python/tree/master/azure-servicebus/azure/servicebus . GitHub 上提供了适用于 Python 的 Azure 服务总线 SDK 的源代码: https ://github.com/Azure/azure-sdk-for-python/tree/master/azure-servicebus/azure/servicebus。
You could check the length of the messages returned from peek_messages method on the class azure.servicebus.ServiceBusReceiver
您可以检查从类
azure.servicebus.ServiceBusReceiver
上的peek_messages方法返回的消息的长度
with servicebus_receiver:
messages = servicebus_receiver.peek_messages(max_message_count=1)
if len(messages) == 0:
print('Queue empty')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.