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();
}
}