【发布时间】:2018-08-14 07:28:05
【问题描述】:
我正在使用 Azure 服务总线队列通过批处理读取客户端消息。我正在通过 serviceBusClient.ReceiveBatch 从服务总线队列读取消息并将它们写入 SQL 数据库。在写入 SQL 数据库时,我还在后台线程中批量更新每条消息。假设 SQL Server 关闭了 24 小时,然后它等到 sql 启动并将所有消息写入数据库,然后最终调用 CompleteBatch。同时,批量中的所有消息锁定正在自动更新。 现在我想知道我可以在多长时间内(最多几小时)自动更新消息锁定?
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();
}
}
【问题讨论】:
标签: c# batch-processing azureservicebus