处理邮件时,我不断收到MessageLockLostExceptions
。
LockDuration
设置为30秒的队列,其中已经包含许多要处理的消息。现在,我想通过添加LockDuration
来模拟一个运行时间更长的消息处理任务(但仍在Task.Delay(10_000)
内)。但是,我每收到第4条消息就得到一个MessageLockLostException
。
即使我设置了MaxAutoRenewDuration = TimeSpan.FromDays(30)
和PrefetchCount = 0
,也会发生这种情况。
这是消息处理方法,我稍作更改以打印出剩余的锁定持续时间:
private static async Task processMessagesAsync(Message message, CancellationToken token)
{
Console.Write($"Received message: {message.SystemProperties.SequenceNumber}. Remaining lock duration: {message.SystemProperties.LockedUntilUtc - DateTime.UtcNow}");
await Task.Delay(10000);
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
Console.WriteLine(" - Complete!");
}
示例输出:
======================================================
Press ENTER key to exit after receiving all the messages.
======================================================
Received message: 3659174697238584. Remaining lock duration: 00:00:30.8269132 - Complete!
Received message: 19421773393035331. Remaining lock duration: 00:00:20.5271654 - Complete!
Received message: 11540474045136941. Remaining lock duration: 00:00:10.3372697 - Complete!
Received message: 15762598695796784. Remaining lock duration: 00:00:00.1776760
Message handler encountered an exception Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. Reference:2c6caac3-e607-4130-a522-f75e4636e130, TrackingId:3ff82738-664d-4aca-b55f-ba3900f1c640_B17, SystemTracker:ocgtesting:queue:workflow~63, Timestamp:2018-12-12T17:01:59
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.OnRenewLockAsync(String lockToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 1260
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.<>c__DisplayClass74_0.<<RenewLockAsync>b__0>d.MoveNext() in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 771
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 83
at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\RetryPolicy.cs:line 105
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.RenewLockAsync(String lockToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 773
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.RenewLockAsync(Message message) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\Core\MessageReceiver.cs:line 742
at Microsoft.Azure.ServiceBus.MessageReceivePump.RenewMessageLockTask(Message message, CancellationToken renewLockCancellationToken) in C:\source\azure-service-bus-dotnet\src\Microsoft.Azure.ServiceBus\MessageReceivePump.cs:line 248.
完整代码在这里:https://pastebin.com/sFGBgE0s
答案 0 :(得分:3)
您的repro缺少的一件事是队列描述。重要的是要注意这样的细节,因为您遇到的问题与客户端无关,并且很可能与代理或基础AMQP库有关。
对于非分区队列,此设置可以正常工作。它不适用于分区队列(标准层)。新老客户均可观察到。我已经为Azure Service Bus团队募集了与经纪人相关的issue进行调查。
答案 1 :(得分:0)
Complete
过期之前,您需要Lock Token
消息。锁定令牌过期后,您将在完成操作期间收到MessageLockLostException
。
我看到您为每条消息将线程执行延迟了10秒。但是消息似乎是在同一时间获取的,这就是为什么每个消息剩余的锁定持续时间不断减少的原因。
对于第四条消息,剩余锁定持续时间为00:00:00.1776760
。因此,在177 milliseconds
之后,锁定将失效。您在下一行中将线程延迟10 seconds
。因此,该锁将过期,并且您将获得MessageLockLostException
。为避免此异常,请删除
Delay