【发布时间】:2020-03-29 12:06:04
【问题描述】:
每隔一段时间(千分之一的消息)我在尝试完成消息时收到 MessageLockLostException。
我定义了一个队列,锁定持续时间设置为 30 秒:
在代码中,我使用以下选项创建了一个侦听器:
var messageHandlerOptions = new MessageHandlerOptions(exceptionReceivedHandler)
{
MaxConcurrentCalls = 10,
AutoComplete = false,
MaxAutoRenewDuration = TimeSpan.FromSeconds(180)
};
var queueClient = ResolveQueueClientByQueueConfigName(queueConfigName);
_logger.Info($"Listening on queue {queueClient.ServiceBusConnection.Endpoint}{queueClient.QueueName}");
queueClient.RegisterMessageHandler(MyMessageHandler, messageHandlerOptions);
并且在 MyMessageHandler 中我在成功处理后完成消息:
private static async Task CompleteAsync(IReceiverClient queueClient, string lockToken)
{
await queueClient.CompleteAsync(lockToken);
}
private async Task MyMessageHandler(Message message, CancellationToken cancellationToken)
{
//Processing omitted
await CompleteAsync(queueClient, message.SystemProperties.LockToken);
}
就像我说的,这适用于大多数消息,但偶尔我会收到以下信息:
2020-03-27 10:31:01,004 [64] INFO Received message: MessageId:7da4f7ec-9e27-4951-83f5-7f29d8fc93a8
2020-03-27 10:31:01,005 [64] INFO Entity Framework Core "3.1.0" initialized '"SmsDataContext"' using
provider '"Microsoft.EntityFrameworkCore.SqlServer"' with options: "None"
2020-03-27 10:31:01,087 [109] INFO Sending message to smssender, CorrelationId: 7da4f7ec-9e27-4951-83f5-7f29d8fc93a8, label: SmsSender
2020-03-27 10:31:05,205 [158] INFO Completing message '7da4f7ec-9e27-4951-83f5-7f29d8fc93a8'
2020-03-27 10:31:05,206 [158] ERROR 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, or was received by a different
receiver instance.
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.DisposeMessagesAsync(IEnumerable`1 lockTokens,
Outcome outcome)
at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.CompleteAsync(IEnumerable`1 lockTokens)
at Bcp.AzureServiceBusHelper.ServiceBusClientHelper.CompleteAsync(String queueName, Message message)
at Bcp.Sms.Validator.ServiceHost.HostedService.ProcessMessagesAsync(Message validatorMessage,
CancellationToken token) in ***
at Microsoft.Azure.ServiceBus.MessageReceivePump.MessageDispatchTask(Message message). Context:
Endpoint= prdhybrid.servicebus.windows.net; Entity Path: smsrequestvalidation; Executing Action:
UserCallback
2020-03-27 10:31:30,949 [109] INFO Received message: MessageId:7da4f7ec-9e27-4951-83f5-7f29d8fc93a8
因此,在这种情况下,锁定是在 10:31:01 获得的,而消息完成在 4 秒后的 10:31:05 失败,正好在 30 秒的锁定持续时间内。
最后一条日志行表明消息在 10:31:30 30 秒后重试,这告诉我锁应该在 10:31:05 有效。
会不会是message.SystemProperties.LockToken中包含的锁被破坏了? 另外,这可能是由于短暂的网络中断造成的吗?我想 LockToken 在这种情况下仍然有效。
此应用程序使用 Microsoft.Azure.ServiceBus 4.1.2 在 .net core 3.1 中编写
提前感谢您的帮助!
【问题讨论】:
标签: azure azureservicebus