0

我使用 Azure 服务总线队列来接收消息并开始一个长时间运行的活动,该活动可以持续几分钟到几个小时。在活动期间,一个单独的线程每 30 秒更新一次锁,直到活动完成。在 BrokeredMessage.RenewLock() 上,发生异常,下面的完整跟踪:“Microsoft.ServiceBus.Messaging.SessionLockLostException”(这是第一次发生)

这是更新锁的代码

Timer resetToken = new System.Threading.Timer (e => RenewLockQueueJobMessage (), null, TimeSpan.Zero, TimeSpan.FromSeconds (30));
private void RenewLockQueueJobMessage ()
{
   brokeredMessage.RenewLock ();
 }

队列的配置:

QueueDescription queueDescription = new QueueDescription (queueName);
queueDescription.EnablePartitioning = true;
queueDescription.RequiresSession = true;
queueDescription.RequiresDuplicateDetection = true;
queueDescription.EnableDeadLetteringOnMessageExpiration = true;
queueDescription.DefaultMessageTimeToLive = TimeSpan.MaxValue;
queueDescription.LockDuration = TimeSpan.FromMinutes (1);
var manager = NamespaceManager.CreateFromConnectionString (connectionString);
manager.CreateQueue (queueDescription);

异常跟踪:

Microsoft.ServiceBus.Messaging.SessionLockLostException: Channel:uuid:;Link: TrackingId:, SystemTracker:net.tcp:,  ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:
   at Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.ThrowIfFaultMessage(Message wcfMessage)
   at Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.HandleMessageReceived(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at 

    Microsoft.ServiceBus.Messaging.Sbmp.DuplexRequestBindingElement.DuplexRequestSessionChannel.EndRequest(IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.RequestAsyncResult.<>c.<GetAsyncSteps>b__9_3(RequestAsyncResult thisPtr, IAsyncResult r)
       at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.EndRequest(IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Sbmp.RedirectBindingElement.RedirectContainerChannelFactory`1.RedirectContainerSessionChannel.RequestAsyncResult.<>c__DisplayClass8_1.<GetAsyncSteps>b__4(RequestAsyncResult thisPtr, IAsyncResult r)
       at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Sbmp.RedirectBindingElement.RedirectContainerChannelFactory`1.RedirectContainerSessionChannel.EndRequest(IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.RequestAsyncResult.<>c.<GetAsyncSteps>b__9_3(RequestAsyncResult thisPtr, IAsyncResult r)
       at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Channels.ReconnectBindingElement.ReconnectChannelFactory`1.RequestSessionChannel.EndRequest(IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageReceiver.RenewLockAsyncResult.<>c.<GetAsyncSteps>b__10_1(RenewLockAsyncResult thisPtr, IAsyncResult a)
       at Microsoft.ServiceBus.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult result)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.Sbmp.SbmpMessageReceiver.OnEndRenewMessageLocks(IAsyncResult result)
       --- End of inner exception stack trace ---
       at Microsoft.ServiceBus.Common.ExceptionExtensions.ThrowException(Exception exception)
       at Microsoft.ServiceBus.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.ReceiveContext.EndRenewLock(IAsyncResult result)
       at Microsoft.ServiceBus.Messaging.ReceiveContext.EndRenewLock(IAsyncResult result)
4

1 回答 1

0

您的队列需要一个会话并使用分区实体。SessionLockLostException迟早会发生,您应该重新尝试接收和处理。如果您向外扩展,消息将再次被拾取并由消费者或另一个实例重新处理。

此外

  1. 您可以更改MaxLockDuration为 5 分钟并减少计时器频率以发出锁更新。
  2. 考虑使用具有自动更新功能的 OnMessage API 来简化代码中的锁更新和异常处理。此示例可以提供帮助。
于 2018-01-18T14:47:12.060 回答