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 brokeredmessage and one or more consumers consume the brokeredmessage from the queue as shown below.

Very often, we come into the situation to read the brokeredmessage from a queue and process the brokeredmessage 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

//Create a CTS to launch a task in charge of renewing the message lock
var brokeredMessageRenewCancellationTokenSource = new CancellationTokenSource();

try {
 var brokeredMessage = _client.Receive();

 var brokeredMessageRenew = Task.Factory.StartNew(() => {
  while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested) {
   //Based on LockedUntilUtc property to determine if the lock expires soon
   if (DateTime.UtcNow > brokeredMessage.LockedUntilUtc.AddSeconds(-10)) {
    // If so, repeat the message

 }, brokeredMessageRenewCancellationTokenSource.Token);

 // Performing a lengthy operation

 // Mark the message as completed
} catch (MessageLockLostException) {
 // lock expired
} catch (Exception) {
} finally {
 // Cancel the lock of renewing the task

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.