Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a DeadLetter message on an Azure Service Bus Topic

I'm writing a piece of code which will allow us to:

  1. View a list of all dead letter messages that exist within an Azure Service Bus Topic (Peek)
  2. Fix and send them back to the Topic
  3. Delete them from the dead letter queue upon resending.

I have no issues with the first 2 points; using the Peek receive mode I can show a list of messages and we can edit and resend with no issues.

The problem comes when I want to actually delete the message from the dead letter queue.

How do we do this on a message by message level? We may only want to delete 2 of the messages residing in the dead letter queue and keep the others for reviewing at a later stage. Does calling .Complete() on a message in the dead letter queue remove it like it does in the main subscription?

For reference; here is our code for getting the SubscriptionClient for the dead letter queue:

private SubscriptionClient GetOrCreateSubscriptionClient(string connectionString)
{
    if (!NamespaceManager.TopicExists(_topicName))
    {
        NamespaceManager.CreateTopic(new TopicDescription(_topicName)
        {
            MaxSizeInMegabytes = 5120,
            DefaultMessageTimeToLive = TimeSpan.FromSeconds(DEFAULT_LOCK_DURATION_IN_SECONDS)
        });
    }

    if (!NamespaceManager.SubscriptionExists(_topicName, _subscriptionName))
    {
        NamespaceManager.CreateSubscription(_topicName, _subscriptionName);
    }

    var deadLetterPath = SubscriptionClient.FormatDeadLetterPath(_topicName, _subscriptionName);

    var client = SubscriptionClient.CreateFromConnectionString(
        connectionString, deadLetterPath, _subscriptionName, ReceiveMode.PeekLock);

    return client;

}
like image 996
Luke Merrett Avatar asked Dec 04 '13 14:12

Luke Merrett


People also ask

How do I check messages in Service Bus topic?

Peek a message To peek messages, select Peek Mode in the Service Bus Explorer dropdown. Check the metrics to see if there are Active Messages or Dead-lettered Messages to peek and select either Queue / Subscription or DeadLetter sub-queue. Select the Peek from start button.

What is a dead-letter message?

The purpose of the dead-letter queue is to hold messages that can't be delivered to any receiver, or messages that couldn't be processed.


2 Answers

Updated code to delete a message from the dead letter queue using the new ServiceBusClient from azure.messaging.servicebus:

public async Task DeleteDeadLetterMessageAsync(string connectionString, string queueName, long sequenceNumber)
{
    var serviceBusClient = new ServiceBusClient(connectionString);
    var receiverOptions = new ServiceBusReceiverOptions { SubQueue = SubQueue.DeadLetter };
    var receiver = serviceBusClient.CreateReceiver(queueName, receiverOptions);
    var msg = await receiver.PeekMessageAsync(sequenceNumber);
    if (msg.SequenceNumber == sequenceNumber)
    {
        msg = await receiver.ReceiveMessageAsync();
        await receiver.CompleteMessageAsync(msg);
    }
}
like image 172
pberggreen Avatar answered Oct 29 '22 21:10

pberggreen


Yes, calling complete on the reference to the brokered message you receive from the dead letter queue will remove it from the dead letter queue.

like image 26
MikeWo Avatar answered Oct 29 '22 20:10

MikeWo