Azure Service Bus重试策略不会更改行为 [英] Azure Service Bus Retry Policy doesn't change the behavior

查看:140
本文介绍了Azure Service Bus重试策略不会更改行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解Azure Service Bus上的重试策略,但是它没有按我期望的方式工作.我有以下代码,它们既侦听消息,又将消息发送到特定的天蓝色队列.

I'm trying to understand the retry policy on the Azure Service Bus but it's not working the way I would expect. 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();
        }
    }
}

即使我指定重试策略应重试15次,但我仍然看到它仅重试默认值10次.我什至尝试使用NoRetry策略,但仍重试10次.

Even though I'm specifying that the retry policy should retry 15 times, I'm still seeing it only retry the default 10 times. I've even tried using the NoRetry policy, but it still retries 10 times.

我还验证了队列上的Maximum Delivery Count设置为任意大的数字,但这没有任何改变:

I also verified that the Maximum Delivery Count on the queue was set to an arbitrarily large number, but that didn't change anything:

我确定我将重试策略分配给众多不同的客户/工厂的做法太过分了,但是我不确定这里出了什么问题.

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. I.e. 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.

使用重试策略向ASB客户端指定在放弃之前如何处理瞬态错误,而不是消息可以出队多少次.

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.

这篇关于Azure Service Bus重试策略不会更改行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆