4

I have a spring managed bean of type B. I have @EnableREtry in a @Configuration class. When I use @Retryable on doStuff(), the method gets retried on failure as expected.

But, the method I really want to retry is a method defined in the base class, A. A is a concrete class and not a spring managed bean. the doSomethingElse method doesn't get retried on throwing an exception.

I really want doSomethingElse to be retried, the base class method. However, I'm not sure how to do this. I'm guessing it's because A is a concrete class and not a bean, although it does serve as a base class.

Do I need to use a RetryableTemplate in class A?

public class B extends A {

   public void doStuff() {
      super.doSomethingElse();
   }
}

public class A {
     // doesn't actually retry
    @Retryable
    public void doSomething() {
      throws new Exception();
    }
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176

1 Answers1

25

@Retryable is implemented using Spring AOP.

Only external calls to retryable methods go through the proxy (which invokes the method within a RetryTemplate); internal calls within the class bypass the proxy and therefore are not retried.

You can play some tricks to get a reference to the proxy from the application context and call that, or simply use a RetryTemplate directly within your doStuff() method.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I believe RetryTemplate would be useful here since it would operate on classes that aren't managed by spring (at least, I assume so). – Stealth Rabbi Jan 25 '17 at 16:18