简体   繁体   English

Azure:如何从服务总线队列中删除“死信”消息

[英]Azure: How to Delete “DeadLettered” Messages from Service Bus queue

I would like to delete the Dead Lettered messages from the service bus queue.我想从服务总线队列中删除死信消息。 In particular, this value is called DeadLetterMessageCount and you can find out this by right-clicking the "Properties" of the SB queue in the Server Explorer of your project (in case of using a SB queue).特别是,该值称为 DeadLetterMessageCount,您可以通过在项目的服务器资源管理器中右键单击 SB 队列的“属性”(在使用 SB 队列的情况下)来找到它。

The reason I would like to do this is because I've set up an Autoscale of the cloud service.我想这样做的原因是因为我已经设置了云服务的 Autoscale。 So, when the SB queue is quite big, it adds some more cores in order to proceed the messages faster (it enables more worker roles).因此,当 SB 队列相当大时,它会添加更多内核以更快地处理消息(它启用更多的工作角色)。 I realized that when you set up the scaling depending on the number of messages in the queue, it counts the DeadLettered messages as well (messages that cannot be consumed).我意识到当您根据队列中的消息数量设置缩放时,它也会计算 DeadLettered 消息(无法使用的消息)。 So that's a waste of money, as more instances are enabled that are not needed.所以这是浪费金钱,因为启用了更多不需要的实例。

Any queries, please let me know.有任何疑问,请告诉我。

Thanks for your help谢谢你的帮助

You read and delete messages from dead letter queue the same way you read from normal queues or subscriptions.您从死信队列读取和删除消息的方式与从普通队列或订阅读取消息的方式相同。

You can use this method to get the path of the queue: QueueClient.FormatDeadLetterPath(queuePath) .您可以使用此方法获取队列的路径: QueueClient.FormatDeadLetterPath(queuePath)

Also see this previous answer: How do I delete a DeadLetter message on an Azure Service Bus Topic另请参阅上一个答案: 如何删除 Azure 服务总线主题上的死信消息

This is a code to delete a Dead-Letter messages from Queues.这是从队列中删除死信消息的代码。

public async void DeleteMessagesFromQueueAsync()
    {
        bool isDeadLetter=true;
        long SequenceNumber = 12;
        string queuePath='queue name';
        string connectionString='connection string of ASB Namespace';

        BrokeredMessage _srcMessage = null;
        DeleteMessageResponse _msgDeletionStatus = new DeleteMessageResponse();
        MessageReceiver fromQueueClient = null;
        try
        {
            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

            string _fromEntityPath = !isDeadLetter ? queuePath : QueueClient.FormatDeadLetterPath(queuePath);

            fromQueueClient = await factory.CreateMessageReceiverAsync(_fromEntityPath, ReceiveMode.PeekLock);

                BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);
                if (_message != null)
                    _srcMessage= _message;

            if (_srcMessage != null )
            {  
                    await _srcMessage.CompleteAsync();
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (fromQueueClient != null)
                await fromQueueClient.CloseAsync();
        }
    }

You can use 'ReceiveAndDelete' mode and 'ReceiveBatchAsync' to delete quickly from DeadLetterQueue您可以使用“ReceiveAndDelete”模式和“ReceiveBatchAsync”从 DeadLetterQueue 中快速删除

private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var DLQPath = "/$DeadLetterQueue"; ///**** Important - Pointing to DLQ'

                var topicName = "message";
                var sub = "message-subscription";
                int batchSize = 100;

                runProcess = true;

                _subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionSt, topicName, sub + DLQPath, ReceiveMode.ReceiveAndDelete);

                int cnt = 0;

                do
                {
                    var messages = await _subscriptionClient.ReceiveBatchAsync(batchSize);
                    var msgCount = messages.Count();

                    if (msgCount == 0)
                    {
                        break;
                    }
                    cnt += msgCount;
                    labelCount.Text = cnt.ToString();
                }
                while (runProcess);

                _subscriptionClient.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetBaseException().Message);
                return;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM