【问题标题】:How to retrieve Dead Letter Queue count?如何检索死信队列计数?
【发布时间】:2017-04-15 04:08:05
【问题描述】:

问题

如何在不接收每条消息并计算我收到多少条消息的情况下获得死信队列长度?

我当前的实现

 public int GetDeadLetterQueueCount()
    {
        //Ref:http://stackoverflow.com/questions/22681954/how-do-you-access-the-dead-letter-sub-queue-on-an-azure-subscription

        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(CloudConnectionString);

        QueueClient deadLetterClient = factory.CreateQueueClient(QueueClient.FormatDeadLetterPath(_QueueClient.Path), ReceiveMode.PeekLock);
        BrokeredMessage receivedDeadLetterMessage;

        List<string> lstDeadLetterQueue = new List<string>();

        // Ref: https://code.msdn.microsoft.com/Brokered-Messaging-Dead-22536dd8/sourcecode?fileId=123792&pathId=497121593
        // Log the dead-lettered messages that could not be processed:

        while ((receivedDeadLetterMessage = deadLetterClient.Receive(TimeSpan.FromSeconds(10))) != null)
        {
                lstDeadLetterQueue.Add(String.Format("DeadLettering Reason is \"{0}\" and Deadlettering error description is \"{1}\"",
                receivedDeadLetterMessage.Properties["DeadLetterReason"],
                receivedDeadLetterMessage.Properties["DeadLetterErrorDescription"]));
                var locktime = receivedDeadLetterMessage.LockedUntilUtc;
        }

        return lstDeadLetterQueue.Count;
    }

实施问题

因为我以窥视和阻止模式接收每条消息,所以这些消息设置了锁定持续时间。在此期间,直到该时间段超时,我才能再次接收甚至看到消息。

必须有一种更简单的方法来获取计数而无需轮询队列?

我也不想消费消息,我只想计算总数。

【问题讨论】:

    标签: c# azure-storage message-queue dead-letter message-bus


    【解决方案1】:

    您可以使用NamespaceManager 的GetQueue() 方法,该方法具有MessageCountDetails 属性,而该属性又具有DeadLetterMessageCount 属性。比如:

    var namespaceManager = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString("<CONN_STRING>");
    var messageDetails = namespaceManager.GetQueue("<QUEUE_NAME>").MessageCountDetails;
    var deadLetterCount = messageDetails.DeadLetterMessageCount;
    

    【讨论】:

    • 完美。谢谢。
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 2012-10-06
    • 2012-05-02
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多