Suraj Menon
Suraj Menon

Reputation: 1594

resilience4j annotations not working on chlid class

I am using resilience4j with SpringBoot. I see that the resilience4j annotations work only if they are placed in the class which throws the exception. If the class is extended by another class & the parent class has the annotation then the retries do not work.

Resilience4j Config

resilience4j.retry: 
  instances:       
    service: 
      maxRetryAttempts: 5
      waitDuration: 1000
      retryException: 
        - com.common.exception.RetriableException

Parent Class

@Retry(name = "service")
@Component
public class httpClient extends client{

  // This method is invoked from outside
  public HttpResponse<T> getResponse(
      String url, Class<T> responseType) {
    return super.getResponse(url, requestEntity, responseType);
  }
}

Child Class

@Retry(name = "service") // Without this line, the retries don't work, even though it is present in the parent class
@Component
public class client{

  public HttpResponse<T> getResponse(
      String url, Class<T> responseType) {
    //Impl which throws RetriableException
  }
}

Is this the expected behaviour ? Can you let me know if I am missing something

Upvotes: 0

Views: 1661

Answers (1)

kriegaex
kriegaex

Reputation: 67317

I never used Resilience4j before, but what I can tell you about Java annotations in general is:

  • An overridden method in a subclass never inherits annotations from the original parent class method.
  • In a class implementing an interface an implementing method never inherits annotations from the corresponding interface method.
  • A classes implementing an interface never inherits annotations from the interface itself.
  • An interface extending another interface also never inherits any type or method level annotations.
  • By default not even a subclass inherits annotations from its parent class.
  • There is a single exception to this "annotations are never inherited" rule: A type level annotation (something like @Foo class Base, can also be abstract) can be inherited by a subclass (something like class Sub extends Base) if and only if the annotation class itself carries the meta annotation @Inherited.

Having said that and looking at the @Retry annotation, you will notice that there is no @Inherited annotation there, so it also cannot work in your case.

If there is another way (e.g. via reflection) to get this done in Resilience4j, I do not know because, as I said, I never used it before.

Upvotes: 2

Related Questions