2

I'm trying to implement a retry policy but it keeps getting ignored. Is there properties in SubscriptionClient that overrides the ones I give when I create the client?

Here is the code i tried:

_retryPolicy = new Microsoft.Azure.ServiceBus.RetryExponential(
                       TimeSpan.FromMinutes(2), // MinBackOff
                       TimeSpan.FromMinutes(5), // MaxBackOff
                       3); // Max Retries

_subscriptionClient = new SubscriptionClient(
               serviceBusPersisterConnection.ServiceBusConnectionStringBuilder,
               subscriptionClientName, retryPolicy: _retryPolicy);

So this should make it only retry 3 times with minimun 2min interval. Result from LOG:

28 May 2019 16:34:49.928 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:49.285 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:48.718 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:48.075 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:47.499 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:46.965 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:46.511 MessageId: 1d327c9033de4fc69892766084264
28 May 2019 16:34:45.957 MessageId: 1d327c9033de4fc69892766084264

As you can see it retried 8 times instead of 3 and the time between retries (this is my main problem) has an interval of half second.

To be sure it was not my retry policies i tried this instead:

_subscriptionClient = new SubscriptionClient(
              serviceBusPersisterConnection.ServiceBusConnectionStringBuilder,
              subscriptionClientName, retryPolicy: RetryPolicy.NoRetry);

And result was exactly the same, so i'm sure it's being ignored. Any help would be much appreciated.

Keep up the good work.

Miguel Vale
  • 59
  • 11
  • Retry policy only applies when there is an error when attempting to perform an operation. The underlying system will attempt to retry the operation without raising an exception. If an operation is successful, eg, a successful read, then the retry would not apply. Even if the message is abandoned later in your code. – Kami May 28 '19 at 16:47
  • @Kami thank you for replying. I'm throwing an Exception on pourpouse on my code to validate exactly the moment there is an error. Didn't post that block but rigth after i catch the message i throw an exception. – Miguel Vale May 28 '19 at 16:51
  • The retry mechanism would not apply if you are throwing an exception after the underlying class returns the message. As far as the service bus is concerned, you have asked for a message, it has given you a message and you choose to do nothing with it. See - https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-exceptions and related https://learn.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific#service-bus – Kami May 28 '19 at 16:56

1 Answers1

1

RetryExponential is intended to be used by the Service bus 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 DQed.

Use retry policy to specify to the Service bus client how to deal with transient errors prior to giving up, not how many times a message can be dequeued.

you can read more about here.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • thank you for your awnser. I understand what you guys are saying that this RetryExponential only works for transient erros and not when an Exception is raised. But still there is something doing retries and i have no control over it. Because in my LOG there is a retry happening when i force an Exception, it tries 8 times with halfsecond interval. How can I control this retry that is happening? – Miguel Vale May 29 '19 at 14:21
  • @MiguelVale The number of retries is determined by the `MaxDeliveryCount` property and the interval between these retries is determined by the client on how often it polls for data. – Kami May 31 '19 at 12:54
  • Thanks for the help guys, we managed to implement our own retrypolicy so we can control what to do with messages after exception is raised. – Miguel Vale May 31 '19 at 14:30