如何检查 Azure 存储队列是否有消息

How to check if an Azure storage queue has messages

我正在尝试同时使用一个 WebJob 和一个 Worker Role。

WebJob 将有一个 BlobTrigger,每次将 blob 添加到容器时,都会将一条新消息添加到 Azure 存储队列(称之为 pending blobs)。

此外,还有一个辅助角色,它将汇集来自 待定 blob 队列的消息,并将 blob 名称添加到内部阻塞集合中,该集合将由由 Worker Role 触发的几个任务。

我考虑过在这个解决方案中考虑可伸缩性,因为会有很多 blob 到达容器,所以我不希望出现 CPU 消费高峰。

在开发解决方案时,我想到了一些问题:

如果您使用 Azure Functions 来处理您的队列消息,也许会更好。它只会在队列中出现新消息时触发。

https://azure.microsoft.com/en-us/documentation/articles/functions-bindings-storage/

Is there a way to check if the Azure Storage Queue has messages inside?

队列有一个 ApproximateMessageCount 属性 你可以检查队列深度(注意:这不是 100% 准确,因为检查时消息可能 added/deleted)。

If I call the GetMessage method and the queue does not have any message the excecution will be blocked until a new message arrive?

GetMessage() 是非阻塞的。如果没有消息,则调用returns。注意:由于您计划在工作者角色中创建自己的 reader,因此在处理空队列时要小心:如果您将自己置于一个紧密的循环中并继续爆破队列,您 运行 耗尽队列 2000 transaction/second 限制的风险(您可能会看到过多的网络流量和 cpu 利用率)。如何实施退避策略取决于您,但您需要合并某种类型的退避。

CloudQueue中的FetchAttributes方法class可用于获取Queue的不同属性。计数属性是这些属性之一。

根据documentation

ApproximateMessageCount 属性 returns FetchAttributes 方法检索的最后一个值,没有调用队列服务。