繁体   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