How does spring-retry determine which methods to retry when @Retryable is placed at the class level?

50 views Asked by At

I have a @Retryable placed at the class level.

@Import({GrpcClientRetryConfig.class})
@Retryable(interceptor = "grpcClientRetryInterceptor")
@Component
public class Profile {
    public void method1() {
        method2();
    }

    public void method2() {
        val msg = "Hello World";
        System.out.println(msg);
        throw new RunTimeException("Testing");
    }
}

@EnableRetry
@Configuration
public class GrpcClientRetryConfig {
 @Bean
 public RetryOperationsInterceptor grpcClientRetryInterceptor() {
    val template = new RetryTemplate();
    val backOffPolicy = new ExponentialBackOffPolicy();
    // Set the exponential backoff parameter
    val interceptor = new RetryOperationsInterceptor();
    val simpleRetry = new SimpleRetryPolicy();
    simpleRetry.setMaxAttempts(2);
    template.setRetryPolicy(simpleRetry);
    template.setBackOffPolicy(backOffPolicy);
    template.setListeners(new GrpcClientRetryListener[] {new GrpcClientRetryListener()});
    interceptor.setRetryOperations(template);
    return interceptor;
 }
}

Case 1

val profile = new Profile();
profile.method2(); // Hello World printed twice an exception is thrown

Case 2

val profile = new Profile();
profile.method1(); // Hello World printed twice and then an exception is thrown

A sample project is placed here. Please check com.example.helloworld.controller.HelloWorldController#sendGreetings

Should not Hello World be printed 4 times? method1() calls method2(). method2() gets retried 2 times and then throws the exception to method1() which again calls method2() one more time?

Can someone let me know why method1() is not retried twice in the second case? How does spring determine which methods to be retried when a @Retryable is placed at the class level?

0

There are 0 answers