簡體   English   中英

Azure 服務總線重試選項不起作用

[英]Azure Service Bus Retry Options Not Working

運氣不好,我嘗試配置我的 ServiceBusClient 以重試固定延遲為 10 秒的消息。 我還嘗試了指數重試配置。 但是,代碼總是在一秒或兩秒內重試消息並完全忽略配置。 它甚至忽略了 MaxRetries 並且只重試 10 次,該值在 Azure Portal 中為隊列配置。 我究竟做錯了什么?

我正在使用Azure.Messaging.ServiceBus庫,NuGet package 7.0.0。

代碼:

ServiceBusClient client = new ServiceBusClient(serviceBusConnectionString, new ServiceBusClientOptions()
            {
                RetryOptions = new ServiceBusRetryOptions()
                {
                    Mode = ServiceBusRetryMode.Fixed,
                    Delay = TimeSpan.FromSeconds(10),
                    MaxDelay = TimeSpan.FromMinutes(3),
                    MaxRetries = 30
                }
            });

ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());

// throwing an exception in MyMessageHandlerAsync on purpose
// to test out the retries configuration
processor.ProcessMessageAsync += MyMessageHandlerAsync;

// The uncaught exception causes this method to execute. 
// Processing is attempted 10 times with
// virtually no delay between each attempt.
// After the 10th attempt, the message goes to deadletter,
// which is expected.
processor.ProcessErrorAsync += MyErrorHandler;

收到第一個回復后,我在這個問題上添加了更多內容:

目前,MyMessageHandlerAsync 是:

    private async Task MyMessageHandlerAsync(EventArgs eventArgs)
    {
        var args = (ProcessMessageEventArgs)eventArgs;

        var body = args.Message.Body.ToString();
        
        // ...
        // process body
        // ...

        await args.CompleteMessageAsync(args.Message);
    }

我應該如何更改方法的內容以重試非瞬態 ServiceBusException? 請幫助提供以下 TODO 的代碼:

    private async Task MyMessageHandlerAsync(EventArgs eventArgs)
    {
        var args = (ProcessMessageEventArgs)eventArgs;

        try
        {

            var body = args.Message.Body.ToString();
        
            // ...
            // process body
            // ...

            await args.CompleteMessageAsync(args.Message);
        }
        catch (ServiceBusException sbe)
        {
            if (sbe.IsTransiet)
            {
                // TODO: Is it correct that the exponential retry will work
                // here?  The one defined in the ServiceBusClient.
                // So, no code is needed here, just throw.
                throw;
            }
            else
            {
                // TODO: for non-transient, this is where the
                //       options in the ServiceBusClient don't apply.
                //       Is that correct?  How do I do an
                //       exponential retry here?
            }
        }
        catch (Exception e)
        {
            // TODO: same problem as else in first catch.
        }
    }

ServiceBusRetryOptions旨在供 ASB 客戶端在存在不會立即冒泡到您的代碼的暫時性錯誤時使用,即客戶端內置的內部重試機制,在引發異常之前代表您執行重試。

使用重試策略向 ASB 客戶端指定在放棄之前如何處理暫時性錯誤,而不是消息處理程序拋出錯誤的次數:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM