简体   繁体   English

Azure Service Bus重试策略不会更改行为

[英]Azure Service Bus Retry Policy doesn't change the behavior

I'm trying to understand the retry policy on the Azure Service Bus but it's not working the way I would expect. 我正在尝试了解Azure Service Bus上的重试策略,但是它没有按我期望的方式工作。 I have the following code that both listens for messages and sends a message to a specific azure queue. 我有以下代码,它们既侦听消息,又将消息发送到特定的天蓝色队列。

using System;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusTester
{
    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = "Endpoint=sb://<NamespaceName>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<SharedAccessKey>";
            var queueName = "MyTestQueue";

            var retryPolicy = new RetryExponential(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(30), 15);

            var ns = NamespaceManager.CreateFromConnectionString(connectionString);
            ns.Settings.RetryPolicy = retryPolicy;

            if (!ns.QueueExists(queueName))
                ns.CreateQueue(queueName);

            var mf = MessagingFactory.CreateFromConnectionString(connectionString);
            mf.RetryPolicy = retryPolicy;

            var mr = mf.CreateMessageReceiver(queueName);
            mr.RetryPolicy = retryPolicy;

            var retryCount = 0;

            mr.OnMessage(_ =>
            {
                retryCount++;
                Console.WriteLine($"{retryCount.ToString().PadLeft(4, ' ')} - Message Received: {_.GetBody<string>()}");
                _.Abandon();
            }, new OnMessageOptions() { AutoComplete = true });


            var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
            client.RetryPolicy = retryPolicy;

            var message = new BrokeredMessage("This is a test message!");

            client.Send(message);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

Even though I'm specifying that the retry policy should retry 15 times, I'm still seeing it only retry the default 10 times. 即使我指定重试策略应重试15次,但我仍然看到它仅重试默认值10次。 I've even tried using the NoRetry policy, but it still retries 10 times. 我什至尝试使用NoRetry策略,但仍重试10次。

控制台输出

I also verified that the Maximum Delivery Count on the queue was set to an arbitrarily large number, but that didn't change anything: 我还验证了队列上的“ Maximum Delivery Count ”被设置为任意大的数字,但这没有任何改变:

在此处输入图片说明

I'm sure I've gone overboard with the assignment of the retry policy to the numerous different clients / factories, but I'm not sure what's wrong here. 我确定我将重试策略分配给众多不同的客户/工厂的做法过分,但是我不确定这里出了什么问题。

RetryExponential is intended to be used by the ASB client when there are transient errors that are not bubbled up to your code right away. 当存在瞬态错误没有立即出现在您的代码中时,ASB客户端将使用RetryExponential Ie an internal retry mechanism built into the client to perform retries on your behalf before exception is raised. 即客户端内置的内部重试机制,可以在引发异常之前代表您执行重试。 If there are no exceptions and your callback Abandons the message explicitly, retry policy is not even utilized here and the message just goes through the normal delivery up to MaxDeliveryCount times (50 in your scenario), after which is DLQed. 如果没有异常, 并且您的回调显式放弃了该消息,则此处甚至不使用重试策略,并且该消息仅经过正常传递,直到MaxDeliveryCount次(在您的方案中为50次),之后是DLQed。

Use retry policy to specify to the ASB client how to deal with transient errors prior to giving up, not how many times a message can be dequeued. 使用重试策略向ASB客户端指定在放弃之前如何处理瞬态错误,而不是消息可以出队多少次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM