简体   繁体   English

如何检查 Azure 服务总线队列是否为空?

[英]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.

相关问题 如何轮询 azure 服务总线队列的消息? - How to poll the azure service bus queue for messages? 如何关闭与 azure 服务总线队列的连接? - How to close connection with the azure service bus queue? 如果 Azure 服务总线队列触发器功能失败,如何将队列消息返回到 Azure 服务总线队列 - How to return queue message to Azure Service Bus Queue if Azure Service Bus Queue Trigger Func Fails 使用python在Azure服务总线中实现队列 - Implementation of a queue in Azure service bus using python Python Azure 服务总线未从队列中检索消息 - Python Azure Service Bus Not retrieving messages from queue Azure 服务总线队列发送和接收消息的测试 - Test of sending & receiving message for Azure Service Bus Queue Celery + Azure Service Bus (Broker) = 声明为空或令牌无效 - Celery + Azure Service Bus (Broker) = claim is empty or token is invalid 如何将 pandas dataframe 发送到 azure 服务总线 - how to send a pandas dataframe to azure service bus 具有优先级的Azure Service Bus队列并处理docker容器以处理队列消息 - Azure Service Bus queue with priority AND Handling docker containers to process the queue messages 当您在队列中写入并等待您在另一个使用服务总线 azure 的响应时 - as you write in a queue and waiting for your response on another using service bus azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM