简体   繁体   中英

Dead letter queue not cleared

I have a few messages that were dead lettered automatically due to an exceeded retry count. Now I'm trying the re-queue them like this:

while ((msg = DeadLetterQueueClient.Receive()) != null)
{
    var body = msg.GetBody<MyModel>();
    QueueClient.Send(new BrokeredMessage(body));
}

I receive all dead lettered messages, however they don't clear from the queue. Every time I start this code, the same messages are received again.

What's wrong?

PS: The queue actually has dead lettering turned off, according to the old Azure portal. The messages only show up as dead lettered in Visual Studio Server Explorer and I also receive them with the above code.

Mark the message as complete and it should be deleted from the dead letter queue

while ((msg = DeadLetterQueueClient.Receive()) != null)
{
    var body = msg.GetBody<MyModel>();
    QueueClient.Send(new BrokeredMessage(body));
    msg.Complete();
}

如果要保留标题属性,则可能要使用msg.Clone()而不是将正文复制到新的BrokeredMessage中。

You can also try this code to delete a Dead-Lettered message from Queues.

  MessageReceiver fromQueueClient = null;

        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
        fromQueueClient = await factory.CreateMessageReceiverAsync(_entityName, ReceiveMode.PeekLock);

            BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);

                await _message.CompleteAsync();
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Azure.ServiceBus;
    using Microsoft.Azure.ServiceBus.Core;
    using System.Text;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    namespace ClearDeadLetterQ

{
    [TestClass]
    public class UnitTest1
    {

        const string ServiceBusConnectionString = "Endpoint=sb://my-domain.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=yoursharedaccesskeyhereyoursharedaccesskeyhere";


        [TestMethod]
        public async Task TestMethod1()
        {
            await this.ClearDeadLetters("my.topic.name", "mysubscriptionname/$DeadLetterQueue");
        }

        public async Task ClearDeadLetters(string topicName, string subscriptionName)
        {
            var messageReceiver = new MessageReceiver(ServiceBusConnectionString, EntityNameHelper.FormatSubscriptionPath(topicName, subscriptionName), ReceiveMode.PeekLock);
            var message = await messageReceiver.ReceiveAsync();
            while ((message = await messageReceiver.ReceiveAsync()) != null)
            {
                await messageReceiver.CompleteAsync(message.SystemProperties.LockToken);
            }
            await messageReceiver.CloseAsync();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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