Azure服务总线:如何续订锁?

问题描述

如何更新“接收队列消息处理程序”上的锁定? 在事件处理程序上,测试消息不具有续订锁定属性

Message testMessage;

enter image description here

https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.brokeredmessage.renewlock?view=azure-dotnet

解决方法

您在上面发布的RenewLock api链接来自旧的不赞成使用的WindowsAzure.ServiceBus nuget包,其中RenewLock方法是BrokeredMessage的一部分。 enter image description here

当前软件包Microsoft.Azure.ServiceBus(您正确使用的)具有RenewLockAsync方法作为接收器https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.core.messagereceiver.renewlockasync?view=azure-dotnet的一部分。您可以从QueueClient实例中调用该方法,例如queueClient.RenewLockAsync(testMessage)queueClient.RenewLockAsync(message.SystemProperties.LockToken)enter image description here

但是,除了手动进行繁琐的工作外,您还可以通过设置MessageHandlerOptions的MaxAutoRenewDuration属性来利用自动更新锁定功能。那将在this example中的方法RegisterOnMessageHandlerAndReceiveMessages中。

        static void RegisterOnMessageHandlerAndReceiveMessages()
        {
            // Configure the MessageHandler Options in terms of exception handling,number of concurrent messages to deliver etc.
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
            {
                // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`,set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
                // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
                AutoComplete = false,// https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-transfers-locks-settlement#peeklock
                MaxAutoRenewDuration = <some timespan>
            };

            // Register the function that will process messages
            queueClient.RegisterMessageHandler(ProcessMessagesAsync,messageHandlerOptions);
        }
,

目前,建议使用 Azure.Messaging.ServiceBus NuGet 包,因为 Microsoft.Azure.ServiceBus 已过时。以下是自动更新处理消息的示例代码:

var client = new ServiceBusClient(connectionString);
var processor = client.CreateProcessor(queueName,new ServiceBusProcessorOptions
{
    MaxAutoLockRenewalDuration = TimeSpan.FromHours(100),});
processor.ProcessMessageAsync += async arg =>
{
    //process your message
    await Task.Delay(Timeout.Infinite);
};
processor.ProcessErrorAsync += async arg =>
{
    //process errors
};

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...