Jump to content

Purge Deferred Messages in Service Bus


Recommended Posts

Guest Mohsin1400
Posted

What are Deferred messages:

 

 

Deferred messages refer to messages that a queue or subscription client is unable to process at the moment due to certain circumstances. Instead of processing it immediately, the client can defer the retrieval of the message to a later time, while the message remains in the queue or subscription.

 

 

 

Message Deferral | Azure Service Bus

 

 

 

Unlike dead-letter messages that are stored in a subqueue, deferred messages are kept in the main queue along with other active messages. However, these messages cannot be received using regular receive operations. If an application loses track of a deferred message, it can be discovered by browsing through the messages.

 

 

 

The responsibility of retrieving a deferred message lies with its owner, who must remember the sequence number as it is deferred. A receiver can later retrieve the deferred message by using receive methods that require the sequence number as a parameter. For further details about sequence numbers, please refer to Message sequencing and timestamps.

 

 

 

However, it can be very difficult or even unfeasible to get each sequence number from the queue/subscription when the entity contains thousands of messages.

 

 

 

Here is an example of how you can receive or purge all the deferred messages in the entity.

 

 

 

Pre-requisites:

 


  • Service Bus namespace
     
     

  • Already created queue/subscription
     
     

  • Service Bus Explorer
     

 

 

 

Using Service Bus Explorer:

 


  1. Download the “Service Bus Explorer” from: GitHub - paolosalvatori/ServiceBusExplorer: The Service Bus Explorer allows users to connect to a Service Bus namespace and administer messaging entities in an easy manner. The tool provides advanced features like import/export functionality or the ability to test topic, queues, subscriptions, relay services, notification hubs and events hubs.
     
     

  2. Open service bus explorer and click File and connect it.
     

 

 

 

mediumvv2px400.png.01a8cd417c776812b8b5ce35e6982767.png

 

 

 

3. From the drop down, select connection string and provide the connection string of the namespace level.

 

 

 

mediumvv2px400.png.f7e1b91cdf3e67a101731619358e8df4.png

 

 

 

4. Once it is successfully connected, you will see Service Bus Explorer shows the count of Active messages as shown below.

 

 

 

mediumvv2px400.png.21f302121947bc5ec4a1b2817d8f363e.png

 

 

 

mediumvv2px400.png.4c12c9869024321b33d5799ad1f729be.png

 

 

 

 

 

5. When we peek through the messages using Service bus explorer we can see the status of the messages as Deferred.

 

 

 

mediumvv2px400.png.cdbfefe05fb0d0d89a51a43e900d7aa8.png

 

 

 

6. When you click on Purge messages, you will notice that the application keeps loading and the messages are not purged.

 

 

 

mediumvv2px400.png.654424bf5b8bac8e26a8b1b09cd347d6.png

 

 

 

 

 

Receive/Delete messages using C# Code:

 

 

 

Run the below code which will receive and complete all the messages from the mentioned queue/subscription after changing the status of deferred to active.

 

 

 

 

 

using Azure.Messaging.ServiceBus;

using Microsoft.Azure.Amqp;

 

class Program

{

static void Main(string[] args)

{

receiveDeferredMessages();

}

public static async Task receiveDeferredMessages()

{

List<long> sequencenumbers;

string connectionString = "SAS Key";

string queueName = "QueueName";

 

try

{

bool condition = true;

sequencenumbers = new List<long>();

await using var client = new ServiceBusClient(connectionString);

ServiceBusReceiver receiver = client.CreateReceiver(queueName);

 

while (condition)

{

ServiceBusReceivedMessage peekedMessage = await receiver.PeekMessageAsync();

if (peekedMessage != null && peekedMessage.State.ToString() == "Deferred")

{

sequencenumbers.Add(peekedMessage.SequenceNumber);

}

else

{

condition = false;

}

}

 

var deferredMessage = await receiver.ReceiveDeferredMessagesAsync(sequencenumbers);

 

foreach (var message in deferredMessage)

{

await receiver.CompleteMessageAsync(message);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

}

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...