[英]Azure Service Bus: How to Renew Lock?
如何更新接收隊列消息處理程序的鎖定? 在事件處理程序上,測試消息沒有更新鎖定屬性。
Message testMessage;
您在上面發布的 RenewLock api 鏈接來自舊的已棄用的 WindowsAzure.ServiceBus nuget 包,其中 RenewLock 方法是 BrokeredMessage 的一部分。
當前包 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)
。
但是,您可以通過設置 MessageHandlerOptions 的 MaxAutoRenewDuration 屬性來利用自動更新鎖定功能,而不是手動進行繁重的工作。 在本例中,這將在方法 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
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.