【问题标题】:How do you get the message count for Azure Topic Subscription?如何获取 Azure 主题订阅的消息计数?
【发布时间】:2012-12-07 02:20:37
【问题描述】:

有没有办法获取 Azure 主题订阅的当前消息计数?

我看到 SubscriptionDescription 类有一个 MessageCount 属性,但这个类似乎只用于创建订阅。我看不到为现有订阅检索 SubscriptionDescription 对象的方法。

【问题讨论】:

    标签: azure azureservicebus


    【解决方案1】:

    我找到了我要找的东西:

    var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
    var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
    long messageCount = subscriptionDesc.MessageCount;
    

    【讨论】:

    • 我认为如果您正在寻找订阅计数而不是主题计数,这会有所帮助。 (虽然取决于您的配置可能是相同的。)我正在寻找一种 API,它可以为整个主题(包括所有订阅)提供活动消息计数。
    • @Lucas,您是否获得了您正在寻找的 API 计数?如果找到请分享。
    • 我相信这是针对旧的 windowsazure 库的。新库使用stackoverflow.com/a/53541781/34315 中提到的类
    【解决方案2】:

    接受的答案是在将 .NET Framework 库与命名空间 Microsoft.ServiceBus.Messaging (nuget package) 一起使用时。

    对于命名空间为 Microsoft.Azure.ServiceBus (nuget package) 的 .NET Standard 库,以下代码可以解决问题:

    var managementClient = new ManagementClient(connectionString);
    var runTimeInfo = await managementClient.GetSubscriptionRuntimeInfoAsync(topicPath, subscriptionName); 
    var messageCount = runTimeInfo.MessageCountDetails.ActiveMessageCount;
    

    有关这两个库之间差异的更多详细信息,请参阅Microsoft.ServiceBus.Messaging vs Microsoft.Azure.ServiceBus

    retirement of .NET Standard 为 .NET 5+ 应用程序提供了一个新的命名空间,Azure.Messaging.ServiceBus (nuget package)。对这个包执行相同操作所需的代码是:

    var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
    var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
    var messageCount = runtimeProps.ActiveMessageCount;
    

    【讨论】:

      【解决方案3】:

      Microsoft.Azure.ServiceBus 库现已弃用,取而代之的是 Azure.Messaging.ServiceBus。所以现在这可以通过 Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient 来实现:

      var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
      var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
      var messageCount = runtimeProps.ActiveMessageCount;
      

      【讨论】:

        猜你喜欢
        • 2019-02-12
        • 1970-01-01
        • 2015-08-11
        • 2019-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-29
        相关资源
        最近更新 更多