Renew lock time for BrokeredMessage

Service Bus Queue is an enterprise level messaging platform which supports publish-subscribe pattern to process the messages. A use case is to loosely couple the two different systems (producer and consumer) where one or more producers produce the message and one or more consumers consume the message from the queue as shown below.


Very often, we come into the situation to read the message from a queue and process the message from another queue based on the data in the former queue. The flow is shown below

In step 1, the producer worker role reads the message from the service bus queue and again reads the message from another service bus queue (step 3). During the processing of the message, it takes long time (probably more than 5 minutes) by the producer role and the lock expiration happens for the message received at step 1. Any operation performs on the message at step 3 now, raises the MessageLockLostException. In order to avoid it, auto-renew pattern needs to be implemented. A sample code is shown below

This code updates RenewLock method of the message lock by resetting the service side timer, and updates the LockedUntilUtc property once RenewLock completes. The entire operation is done in a different thread using Task Parallel Library.