none
ServiceBus RetryPolicy RRS feed

  • Question

  • Hi,

    There is a RetryPolicy property of type Microsoft.ServiceBus.RetryPolicy in the QueueClient class. Should I use this instead of the EntLib Transient Fault Handling block in order to solve transient errors on service bus?

    Thanks!

    Alex


    Thursday, June 27, 2013 4:25 PM

Answers

  • Hi, you can implement your own retry policy as follows:

    public class CustomRetryStrategy : RetryStrategy
     {
      private readonly int retryCount = 3;
      private readonly TimeSpan retryInterval = TimeSpan.FromMilliseconds(1000);
     
      public CustomRetryStrategy()
        : base("customRetryStrategy", true)
      {
        //default values
      }
     
      public CustomRetryStrategy(int retryCount, TimeSpan retryInterval)
        : base("customRetryStrategy", true)
      {
        this.retryCount = retryCount;
        this.retryInterval = retryInterval;
      }
     
      public override ShouldRetry GetShouldRetry()
      {
        if (this.retryCount == 0)
        {
            return delegate(int currentRetryCount, Exception lastException, out TimeSpan interval)
            {
                interval = TimeSpan.Zero;
     
                return false;
            };
        }
     
        return delegate(int currentRetryCount, Exception lastException, out TimeSpan interval)
        {
            if (currentRetryCount < this.retryCount)
            {
                var random = new Random();
                //set random interval within the threshold
                interval = TimeSpan.FromMilliseconds(random.Next((int)(this.retryInterval.TotalMilliseconds * 0.8), (int)(this.retryInterval.TotalMilliseconds * 1.2)));
     
     
    			//use your logic here
    			//....
     
                return true;
            }
     
            interval = TimeSpan.Zero;
     
            return false;
         };
       }
     }

    public class CustomTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
     {
        List<Type> exceptionTypesToRetry;
     
        public CustomTransientErrorDetectionStrategy(List<Type> exceptionType)
        {
            exceptionTypesToRetry = exceptionType;
        }
     
        public bool IsTransient(Exception ex)
        {
            if (exceptionTypesToRetry.Contains(ex.GetType()))
            {
                return true;
            }
            return false;
        }
     }


    Not only for geeks dotnet-geek.co.uk

    Tuesday, July 2, 2013 8:34 PM

All replies

  • here is an article from Microsoft Customer Advisory Team about best practices leveraging azure service bus brokered messaging api

    http://windowsazurecat.com/2011/09/best-practices-leveraging-windows-azure-service-bus-brokered-messaging-api/

    in the "Handling Transient Communication Error" section, they recommend using Transient Fault Handling Framework, also take a look at Transient Fault Handling Application Block

    even Technet article says the same (to be frank looks like a copy of CAT team article)

    http://social.technet.microsoft.com/wiki/contents/articles/best-practices-for-leveraging-windows-azure-service-bus-brokered-messaging.aspx

    here is another good read with some good code samples

    http://www.davidaiken.com/2011/10/10/implementing-windows-azure-retry-logic/

    ---------------------------------------------------

    Please mark as answered if it helped


    Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/

    • Marked as answer by Mang AlexMVP Friday, June 28, 2013 4:16 PM
    • Unmarked as answer by Mang AlexMVP Friday, June 28, 2013 4:22 PM
    • Proposed as answer by Leonid Ganeline Tuesday, July 16, 2013 11:34 PM
    Thursday, June 27, 2013 5:24 PM
  • Hi,

    There are two facts that you are missing though:

    1. The CAT article is almost two years old (it's dated from somewhat 2011).
    2. The RetryPolicy class is new to the Microsoft.ServiceBus namespace, since version 2.1 (dated May 2013)
    3. When it comes to the Storage API, the Enterprise Library team has suggested using the improved RetryPolicy classes in version 2.0, rather than using the ones from the Enterprise Library itself.

    What's you opinion on this?

    Regards,

    Alex

    Friday, June 28, 2013 4:26 PM
  • Hi Mang Alex,

    I understand the article is old but the technique to handle transient errors has not changed much and as always there are multiple ways to address issue at hand, i only suggested the ones that were alternate to Retry Policy class

    In fact the latest version is Enterprise Library 5.0 - Transient Fault Handling Application Block 5.1.1212.0 (it was last updated in 12/2012)

    Here is a code sample for Retry Policy (it was updated by Microsoft in June 2013, so is very very latest)

    http://code.msdn.microsoft.com/windowsazure/How-to-implement-retry-a923a2ea

    I hope this helps

    ---------------------------------------

    Please mark as answered if it helped


    Please mark as answered if it helped Vishal Narayan Saxena http://twitter.com/vishalishere http://www.ogleogle.com/vishal/

    Friday, June 28, 2013 6:04 PM
  • Hi,

    Thanks again for your reply.

    With your permission, allow me to disagree, because, first of all, as of April 2013, Enterprise library is at versio 6.0. it is in this version's documentation, that the patterns & practices team suggests using the Retry Policy class from inside the Storage API for storage related operations. However, it doesn't mention anything about the RetryPolicy class from the ServiceBus API, nor could have it mentioned since the documentation wasn't updated since April and this class was only released this month (June).

    The code sample you've posted is indeed extremely helpful, yet it only relates to transient fault handling on role-based caching operations, which is again, a different scenario.

    My original question was related to the RetryPolicy class from EntLib. vs. the RetryPolicy class from Service Bus API, in regard to Service Bus operations. Basically, I want to find out which one is better, and why. I admit the concept is obviously the same, yet the implementation of Exponential Retry algorithm can be different, the exception handling can be differentn adn features regarding transient detection can also be different. Moreover, there is also the long-term code maintainance fact that has to be taken into account...

    Thanks again for your opinion!

    Regards,

    Alex


    • Edited by Mang AlexMVP Friday, June 28, 2013 9:52 PM typos
    Friday, June 28, 2013 9:50 PM
  • Hi,

    You can learn what EntLib focus on: http://en.wikipedia.org/wiki/Microsoft_Enterprise_Library

    http://msdn.microsoft.com/en-us/library/hh680934(v=pandp.50).aspx

    http://windowsazurecat.com/2011/02/enterprise-library-on-windows-azure-key-learnings-from-customer-project/

    As above said the technique is similar. You can choose anyone without big difference based on my understanding. You just need to reference all EntLib core assemblies if you use EntLib.

    Thanks,


    QinDian Tang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 1, 2013 5:06 AM
  • Hi, you can implement your own retry policy as follows:

    public class CustomRetryStrategy : RetryStrategy
     {
      private readonly int retryCount = 3;
      private readonly TimeSpan retryInterval = TimeSpan.FromMilliseconds(1000);
     
      public CustomRetryStrategy()
        : base("customRetryStrategy", true)
      {
        //default values
      }
     
      public CustomRetryStrategy(int retryCount, TimeSpan retryInterval)
        : base("customRetryStrategy", true)
      {
        this.retryCount = retryCount;
        this.retryInterval = retryInterval;
      }
     
      public override ShouldRetry GetShouldRetry()
      {
        if (this.retryCount == 0)
        {
            return delegate(int currentRetryCount, Exception lastException, out TimeSpan interval)
            {
                interval = TimeSpan.Zero;
     
                return false;
            };
        }
     
        return delegate(int currentRetryCount, Exception lastException, out TimeSpan interval)
        {
            if (currentRetryCount < this.retryCount)
            {
                var random = new Random();
                //set random interval within the threshold
                interval = TimeSpan.FromMilliseconds(random.Next((int)(this.retryInterval.TotalMilliseconds * 0.8), (int)(this.retryInterval.TotalMilliseconds * 1.2)));
     
     
    			//use your logic here
    			//....
     
                return true;
            }
     
            interval = TimeSpan.Zero;
     
            return false;
         };
       }
     }

    public class CustomTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
     {
        List<Type> exceptionTypesToRetry;
     
        public CustomTransientErrorDetectionStrategy(List<Type> exceptionType)
        {
            exceptionTypesToRetry = exceptionType;
        }
     
        public bool IsTransient(Exception ex)
        {
            if (exceptionTypesToRetry.Contains(ex.GetType()))
            {
                return true;
            }
            return false;
        }
     }


    Not only for geeks dotnet-geek.co.uk

    Tuesday, July 2, 2013 8:34 PM