2

I am trying to implement retry logic. my code is working as expected until return type of retry method is void. the moment I change it to String, @Recover stops working.

 @Component
  public class AdapterImpl {
      int count = 0;

    @Retryable(include = {NullPointerException.class, IllegalStateException.class}, backoff = @Backoff(delay = 100, maxDelay = 101), maxAttempts = 5)
    public void retry(String foo) {
        System.out.println(foo + " " + count++);
        if (foo.equals("foo")) {
            throw new NullPointerException("foo");
        } else if (foo.equals("bar")) {
            throw new IllegalStateException("bar");
        }
//        return "hi";
    }

    @Recover
    public void connectionException(NullPointerException e) {
        System.out.println("Retry failure NullPointerException");
    }

    @Recover
    public void connectionException(IllegalStateException e) {
        System.out.println("Retry failure IllegalStateException");
    }
}

for both foo and bar, retry logic is working. logs as below

bar 0
bar 1
bar 2
bar 3
bar 4
Retry failure IllegalStateException

but when I change the return type of retry method to String, the @Recover method stops working.

@Retryable(include = {NullPointerException.class, IllegalStateException.class}, backoff = @Backoff(delay = 100, maxDelay = 101), maxAttempts = 5)
public String retry(String foo) {
    System.out.println(foo + " " + count++);
    if (foo.equals("foo")) {
        throw new NullPointerException("foo");
    } else if (foo.equals("bar")) {
        throw new IllegalStateException("bar");
    }
    return "hi";
}

logs are as below

bar 0
bar 1
bar 2
bar 3
bar 4
2020-04-26 23:28:30.800 ERROR 59644 --- [nio-8087-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is java.lang.IllegalStateException: bar] with root cause

java.lang.IllegalStateException: bar

My build.gradle is

   plugins {
    id 'java'
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}

group 'com.demo.web'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.retry:spring-retry'
    compile 'org.springframework:spring-aspects'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

what am I doing wrong?

shrikant.sharma
  • 203
  • 1
  • 17
  • 37

1 Answers1

8

There are certain rules using @Recover with spring @Retryable

  • Annotation for a method invocation that is a recovery handler.
  • A suitable recovery handler has a first parameter of type Throwable (or a subtype of Throwable) and a return value of the same type as the @Retryable method to recover from.
  • The Throwable first argument is optional (but a method without it will only be called if no others match).
  • Subsequent arguments are populated from the argument list of the failed method in order.

So make sure you have the matching recoverable method with above rules

 @Recover
public String connectionException(NullPointerException e, String foo) {
    System.out.println("Retry failure NullPointerException");
}

@Recover
public String connectionException(IllegalStateException e, String foo) {
    System.out.println("Retry failure IllegalStateException");
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98