'spring-retry : "Getting Retry exhausted after last attempt with no recovery path" with original exception on non retryable method

I am trying to implement spring-retry(version - 1.3.1) in my spring boot application. I have to retry webservice operation to read the record if not found in first request.

sample code:

@Retryable(include = {IllegalArgumentException.class}, backoff = @Backoff(500), maxAttempts = 3, recover ="readFallback")
Object read(String Id);

@Recover
Object readFallback(RuntimeException e, String Id);

void deletePayment(String paymentId);

Problem : I am getting correct response from read method(annotated with @Retryable) in exception scenario but I am getting RetryExhaustedException with nested original exception when I am getting exception on my delete method. As you see, delete method doesn't annotated with @Retryable . Delete method is in different package.

**Sample exception response ** : "Retry exhausted after last attempt with no recovery path; nested exception is exception.NotFoundException: Not found"

Expected : Delete method should not be impacted by @Retryable. Can someone help me to find what am i missing or doing wrong. I have tried but unable to not found the solution of this problem on internet.

Thanks in advance !



Solution 1:[1]

Works as expected for me:

@SpringBootApplication
@EnableRetry
public class So71546747Application {

    public static void main(String[] args) {
        SpringApplication.run(So71546747Application.class, args);
    }

    @Bean
    ApplicationRunner runner(SomeRetryables retrier) {
        return args -> {
            retrier.foo("testFoo");
            try {
                Thread.sleep(1000);
                retrier.bar("testBar");
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        };
    }

}

@Component
class SomeRetryables {

    @Retryable
    void foo(String in) {
        System.out.println(in);
        throw new RuntimeException(in);
    }

    @Recover
    void recover(String in, Exception ex) {
        System.out.println("recovered");
    }

    void bar(String in) {
        System.out.println(in);
        throw new RuntimeException(in);
    }

}
testFoo
testFoo
testFoo
recovered
testBar
java.lang.RuntimeException: testBar
    at com.example.demo.SomeRetryables.bar(So71546747Application.java:52)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:789)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
    at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:166)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
    at com.example.demo.SomeRetryables$$EnhancerBySpringCGLIB$$e61dd199.bar(<generated>)
    at com.example.demo.So71546747Application.lambda$0(So71546747Application.java:26)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:768)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:310)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
    at com.example.demo.So71546747Application.main(So71546747Application.java:17)

Please provide an MCRE that exhibits the behavior you see so we can see what's wrong.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gary Russell