消息计数器Message counters

可以在 .NET Framework SDK 中使用 Azure 资源管理器和服务总线 NamespaceManager API 来检索队列和订阅中含有的消息计数。You can retrieve the count of messages held in queues and subscriptions by using Azure Resource Manager and the Service Bus NamespaceManager APIs in the .NET Framework SDK.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

借助 PowerShell,可以按如下所示获取计数:With PowerShell, you can obtain the count as follows:

(Get-AzServiceBusQueue -ResourceGroup mygrp -NamespaceName myns -QueueName myqueue).CountDetails

消息计数详细信息Message count details

对于确定队列是否累积了需要的处理资源比当前部署资源更多的积压工作,了解活动消息计数会十分有用。Knowing the active message count is useful in determining whether a queue builds up a backlog that requires more resources to process than what has currently been deployed. MessageCountDetails 类提供了以下计数器详细信息:The following counter details are available in the MessageCountDetails class:

如果应用程序要基于队列长度缩放资源,则应按照慎重的进度执行此操作。If an application wants to scale resources based on the length of the queue, it should do so with a measured pace. 获取消息计数器是消息代理中成本高昂的操作,频繁执行它会直接对实体性能产生负面影响。The acquisition of the message counters is an expensive operation inside the message broker, and executing it frequently directly and adversely impacts the entity performance.

备注

发送到服务总线主题的消息将转发到该主题的订阅。The messages that are sent to a Service Bus topic are forwarded to subscriptions for that topic. 因此,主题本身的活动消息计数为 0,因为这些消息已成功转发到订阅。So, the active message count on the topic itself is 0, as those messages have been successfully forwarded to the subscription. 获取订阅处的消息计数,并验证它是否大于 0。Get the message count at the subscription and verify that it's greater than 0. 即使你在订阅中看到消息,但它们实际上存储在主题所拥有的存储中。Even though you see messages at the subscription, they are actually stored in a storage owned by the topic.

如果查看这些订阅,那么它们将具有非零的消息计数(整个实体的空间总计达 323MB)。If you look at the subscriptions, then they would have non-zero message count (which add up to 323MB of space for this entire entity).

后续步骤Next steps

若要了解有关服务总线消息传送的详细信息,请参阅以下主题:To learn more about Service Bus messaging, see the following topics: