如何动态更新很长的处理消息的锁定 - Azure.Messaging.ServiceBus;

发布于 2025-01-17 02:26:52 字数 63 浏览 3 评论 0 原文

是否可以为 Azure.Messaging.ServiceBus 库中正在处理的消息续订锁定,如果可以,如何续订

is it possible to renew lock for a message under process in Azure.Messaging.ServiceBus library, if yes how

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍风燃霜 2025-01-24 02:26:52

根据您的用途,您需要延长锁。使用您提到的服务总线 SDK 提供的处理器,可以通过以下方式完成:

await using var client = new ServiceBusClient(connectionString);

// create the options to use for configuring the processor
var options = new ServiceBusProcessorOptions
{
    MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(45); // Allow message processing for up-to 45 minutes
    AutoCompleteMessages = false,

    // Disable multi-threading for easy debugging
    MaxConcurrentCalls = 1
};

await using ServiceBusProcessor processor = client.CreateProcessor(queueName, options);

processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;

async Task MessageHandler(ProcessMessageEventArgs args)
{
    string body = args.Message.Body.ToString();
    Console.WriteLine(body);

    await args.CompleteMessageAsync(args.Message);
}

Task ErrorHandler(ProcessErrorEventArgs args)
{
    Console.WriteLine(args.ErrorSource);

    Console.WriteLine(args.FullyQualifiedNamespace);
    Console.WriteLine(args.EntityPath);

    Console.WriteLine(args.Exception.ToString());

    return Task.CompletedTask;
}

processor.StartProcessingAsync();
Console.ReadKey();

它们的关键是 MaxAutoLockRenewalDuration 属性需要设置为最大可能时间。

如果您使用 ServiceBusReceiver,它提供RenewMessageLockAsync方法来达到目的。

Depending on what you use, you need to extend the lock. Using the processor provided by the Service Bus SDK you've mentioned, it can be done in the following way:

await using var client = new ServiceBusClient(connectionString);

// create the options to use for configuring the processor
var options = new ServiceBusProcessorOptions
{
    MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(45); // Allow message processing for up-to 45 minutes
    AutoCompleteMessages = false,

    // Disable multi-threading for easy debugging
    MaxConcurrentCalls = 1
};

await using ServiceBusProcessor processor = client.CreateProcessor(queueName, options);

processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;

async Task MessageHandler(ProcessMessageEventArgs args)
{
    string body = args.Message.Body.ToString();
    Console.WriteLine(body);

    await args.CompleteMessageAsync(args.Message);
}

Task ErrorHandler(ProcessErrorEventArgs args)
{
    Console.WriteLine(args.ErrorSource);

    Console.WriteLine(args.FullyQualifiedNamespace);
    Console.WriteLine(args.EntityPath);

    Console.WriteLine(args.Exception.ToString());

    return Task.CompletedTask;
}

processor.StartProcessingAsync();
Console.ReadKey();

They key is the MaxAutoLockRenewalDuration property that needs to be set to the maximum possible time.

In case you're using ServiceBusReceiver, it provides RenewMessageLockAsync method to achieve the goal.

潦草背影 2025-01-24 02:26:52

消息锁定续订应用于由端点主动处理的消息,不适用于预取

消息锁定续订时间应大于LockDuration时间。

LockDuration 期限即将到期时,Azure 服务总线传输将向代理发送锁定续订请求,从而使消息保持锁定状态并且对其他使用者不可见

虽然消息处理的总时间小于 Azure 服务总线传输设置的自动续订时间,但锁定续订将自动发生

锁更新时间默认设置为5分钟。如果更新自动锁定,已处理邮件的 DeliveryCount 不会增加。

为当前正在处理的消息更新消息锁。当传输完成预取消息时,如果未在 LockDuration 时间内处理它们,它们将失去锁定。这将由日志中的 LockLostException 发出信号。通过将 PrefetchCount 设置为零,可以停用预取以避免锁更新异常。

配置消息以锁定续订

var ServiceBusTransport= endpointConfiguration.UseTransport<AzureServiceBusTransport>(); 
var ServiceBusReceivers = ServiceBusTransport.MessageReceivers(); 
ServiceBusReceivers .AutoRenewTimeout(maximumProcessingTime);

请参阅 此处了解更多信息。

Message lock renewal is only applied to messages that are actively processed by endpoints and do not apply to messages that have been prefetched.

The message lock renewal time should be longer than the LockDuration time.

The Azure Service Bus transport will send a lock renewal request to the broker when the LockDuration period is about to expire, keeping the message locked and invisible to other consumers.

While the total time of message processing is less than the auto-renewal time set by Azure Service Bus transport, lock renewal will occur automatically.

The lock renewal time is set to 5 minutes by default. The DeliveryCount of the processed message will not rise if the auto-lock is renewed.

The message lock is renewed for the message that is presently being handled. When prefetched messages are completed by the transport, they will lose their lock if they are not handled within the LockDuration time. This will be signaled by a LockLostException in the log. Prefetching can be deactivated to avoid the lock renewal exception by setting PrefetchCount to zero.

Configuring message to lock renewal

var ServiceBusTransport= endpointConfiguration.UseTransport<AzureServiceBusTransport>(); 
var ServiceBusReceivers = ServiceBusTransport.MessageReceivers(); 
ServiceBusReceivers .AutoRenewTimeout(maximumProcessingTime);

Refer here for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文