Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Using @Retryable in methods define in spring bean's base class are not retried

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();
    }
}

Upvotes: 4

Views: 10380

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

@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.

Upvotes: 26

Related Questions