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?