1
votes

I am using Azure Service Bus Queue for reading client messages through Batch Processing. I am reading messages from Service Bus Queue through serviceBusClient.ReceiveBatch and writing them to SQL Database. While writing to SQL Database,I am also renewing each message in batch in background thread. Suppose SQL Server is down for a 24 hour then it waits till sql is up and writes all messages to database and then finally call CompleteBatch. Meantime all messages' lock in a batch is getting auto renewed. Now I want to know till how much time (maximum in hours) I can auto-renew message lock ?

private void ServiceBusBatchProcessing()
{
    try
    {
        while ((messages = serviceBusClient.ReceiveBatch(100)) != null && messages.Count() > 0)
        {
            brokeredMessageRenewCancellationTokenSource = new CancellationTokenSource();

            var brokeredMessageRenew = Task.Factory.StartNew(() =>
            {
                while (!brokeredMessageRenewCancellationTokenSource.Token.IsCancellationRequested)
                {

                    if (messages.Any(bm => (DateTime.UtcNow > bm.LockedUntilUtc.AddSeconds(-15))))
                    {
                        foreach (var brokeredMessage in messages)
                        {
                            brokeredMessage.RenewLockAsync();
                        }
                    }
                    Thread.Sleep(10000);
                }

            }, brokeredMessageRenewCancellationTokenSource.Token);


            //
            /*

             Code for writing SQL Database goes here.For any 
             exception(SQLConnection Exception)
             this code does nor return but try again till all messages 
             will be written (guaranteed delivery to sql database) 

             */

            serviceBusClient.CompleteBatch(messages.Select(m => m.LockToken));
            brokeredMessageRenewCancellationTokenSource.Cancel();

        }
    }
    catch (MessageLockLostException)
    {
        try
        {
            foreach (var brokeredMessage in messages)
            {
                brokeredMessage.Abandon();
            }
        }
        catch
        {

        }
    }
    catch (Exception ex)
    {
        // brokeredMessage.Abandon();
        if (messages != null)
        {

        }
    }
    finally
    {
        // Cancel the lock of renewing the task
        brokeredMessageRenewCancellationTokenSource.Cancel();
    }
}
2

2 Answers

1
votes

You can renew the Lock Token as many times as you need. The Delivery Count of the message gets incremented only at the time of Receiving the message and not during renewing the Lock Token. So there is no limit on the Lock renewal.

One thing you need to make sure is that you should renew the Token before the expiration.

0
votes

The answer you've already received is correct, unlimitted times. Saying that, I would strongly suggest to review your design. Take this for example:

While writing to SQL Database,I am also renewing each message in batch in background thread. Suppose SQL Server is down for a 24 hour then it waits till sql is up and writes all messages to database and then finally call CompleteBatch. Meantime all messages' lock in a batch is getting auto renewed.

If your SQL Server is down for 24 hours, why would you receive any messages at all? Let along renew their lock token? Remember that operations you're performing cost money. Evenry message you're receiving and renewing its lock counts as an operation. This also complicates your implementation substantially. Another point to raise - lock renewal can and at some point will fail. It's a client-driven command and is not guaranteed.