Auto-renew pattern for messagelocklostexception

sbq-logo

The Azure service bus queue is an enterprise-level messaging platform. It supports a publish-subscribe pattern to process the message. Two different systems can talk to each other using such messaging platform. The producer produces the brokered message and the consumer consumes it as shown in figure 1. Every messaging platform provides a time-to-live (TTL) threshold value of the brokered message. If it goes beyond the value, the brokered message appears in the queue again. In such cases, the client renews the TTL of the brokered message. The brokered message uses a lock feature for this. Messagelocklostexception happens based on a certain scenario. There is a pattern called auto-renew pattern to handle the messagelocklostexception gracefully. In this article, I will share a small code-snippet to auto-renew the lock of a brokered message.
messagelocklostexception-auto-renew-2
Before sharing the code, let me share a situation where messagelocklostexception happens. You need to auto-renew the lock of a brokered message. Let’s consider the below scenario

  • A producer reads a brokered message from a service bus queue (step 1)
  • The same producer reads a brokered message from another service bus queue (step 2). The producer applies some time-consuming logic based on the brokered message retrieved in step 1
  • The producer sends the brokered message retrieved in step 2 to another service bus queue (step 3). Since the logic is time-consuming, the lock expires for the brokered message retrieved in step 2. It eventually raises a messagelocklostexception

messagelocklostexception-auto-renew-1
Let’s refer to the code snippet below to auto-renew the lock of a brokered message
This code updates the RenewLock method of a brokered message lock by resetting the service side timer. It also updates the LockedUntilUtc property once RenewLock completes it. The entire operation is done in a different thread using the task parallel library.

Conclusion

Let me summarise the article now. The key takeaway is to implement an auto-renew pattern when messagelocklostexception happens. The code snippet is a sample one and you can extend it further according to the needs. I hope you like the article. Please leave a comment with your feedback, share it with your friends and colleagues.

One thought on “Auto-renew pattern for messagelocklostexception

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.