Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up deferred messages on Azure Service Bus without the sequence number

Is there any way to recover or delete a deferred message on Azure Service Bus if I've lost the sequence number?

The scenario is: I want to use BrokeredMessage.Defer() to defer a message. I plan to record the sequence number, and use it later on to retrieve the message. But if something goes wrong — let's say some buggy code is deployed — and the sequence number is not recorded properly, it seems like that message will sit on the service bus in a deferred state until the message expires, which could be forever.

This concerns me primarily because that message will occupy space on the queue or subscription, and I haven't found any way of recovering that space short of completely deleting the queue/subscription.

Is there any way to either receive or delete "lost" deferred messages?

like image 891
dshpak Avatar asked Oct 25 '16 19:10

dshpak


People also ask

Should ServiceBusClient be Singleton?

The Service Bus objects that interact with the service, such as ServiceBusClient, ServiceBusSender, ServiceBusReceiver, and ServiceBusProcessor, should be registered for dependency injection as singletons (or instantiated once and shared).

How do I check messages on Azure Service Bus queue?

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.

How do you peek a message on Azure Service Bus?

Azure. ServiceBus package to create a ManagementClient for gathering queue information and then use a MessageReceiver to peek the messages.


2 Answers

Peeking messages from a queue or subscription will also return deferred messages. Deferred messages will have State = "Deferred".

This way you can get the sequence numbers of the deferred messages and afterwards process or delete these messages.

You can try this out in ServiceBus Explorer:

Deferred message in ServiceBus Explorer

like image 166
pberggreen Avatar answered Oct 02 '22 20:10

pberggreen


As to preventing orphaned deferred messages because of lost sequence numbers, you should build resiliency into the storage process where you maintain them, perhaps with another queue. In other words, when you defer a message, create a message in another queue or topic to store the deferred sequence number, or the entire message (cloned).

I typically move all deferred messages to my own "deferred topic", and handle them separately. In other words, my own custom deferral logic, avoiding the problem altogether. If the auto-timeout logic does not work in your scenario (BrokeredMessage.TimeToLive), this may be a better route for you.

This article does a good job of partially explaining your scenario, using a special overload of Queue.Receive:

http://markheath.net/post/defer-processing-azure-service-bus-message

Since you are storing the sequence number (in a resilient way :-)), another alternative would be to store the entire message (JSON, property bag, etc.), and resubmit it when you want to process it again (from another queue, perhaps).

like image 40
DanielG Avatar answered Oct 02 '22 21:10

DanielG