Trying to exclude an exception using @Retryable - causes ExhaustedRetryException to be thrown Trying to exclude an exception using @Retryable - causes ExhaustedRetryException to be thrown spring spring

Trying to exclude an exception using @Retryable - causes ExhaustedRetryException to be thrown


Your include/exclude syntax looks bad - that won't even compile.

I just wrote a quick test and it works exactly as expected if you have zero @Recover methods...

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.retry.annotation.EnableRetry;import org.springframework.retry.annotation.Retryable;@SpringBootApplication@EnableRetrypublic class So38601998Application {    public static void main(String[] args) {        ConfigurableApplicationContext context = SpringApplication.run(So38601998Application.class, args);        Foo bean = context.getBean(Foo.class);        try {            bean.out("foo");        }        catch (Exception e) {            System.out.println(e);        }        try {            bean.out("bar");        }        catch (Exception e) {            System.out.println(e);        }    }    @Bean    public Foo foo() {        return new Foo();    }    public static class Foo {        @Retryable(include = IllegalArgumentException.class, exclude = IllegalStateException.class,                maxAttempts = 5)        public void out(String foo) {            System.out.println(foo);            if (foo.equals("foo")) {                throw new IllegalArgumentException();            }            else {                throw new IllegalStateException();            }        }    }}

Result:

foofoofoofoofoojava.lang.IllegalArgumentExceptionbarjava.lang.IllegalStateException

If you just add

@Recoverpublic void connectionException(IllegalArgumentException e) {    System.out.println("Retry failure");}

You get

foofoofoofoofooRetry failurebarorg.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is java.lang.IllegalStateException

So you need a catch-all @Recover method...

@Recoverpublic void connectionException(Exception e) throws Exception {    System.out.println("Retry failure");    throw e;}

Result:

foofoofoofoofooRetry failurebarRetry failurejava.lang.IllegalStateException