在使用@Async注释的方法中调用@Retryable方法不起作用

时间:2018-04-05 07:25:45

标签: spring spring-boot

下面的@Retryable代码适用于直接调用方法的地方,但是通过@Async注释方法调用的可重试方法然后抛出异常。 有什么建议吗?

这是我的服务类

@Service
public class RetryAndRecoverService {

    int counter = 0;
    String str = null;
    @Retryable(value = {FooException.class, BarException.class}, maxAttempts = 5,  backoff = @Backoff(delay = 1000, multiplier = 1))
    public String retryWithException() {
        System.out.println("retryWithException - "+(counter++));
        String value = getMapperValue();
        if(value == null){
            throw new FooException();
        }
        return value;
    }

     private String getMapperValue() {
        return null;
    }

    @Async
    public String testRetry(){
        return retryWithException();
    }

}

这是Junit Test class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RetryExampleApplication.class)
public class RetryTest {

    @Autowired
    private RetryAndRecoverService retryAndRecoverService;

    @Test
    public void retryWithException() {
        Stream.of(IntStream.range(0, 3)).forEach(index -> {
            String str = retryAndRecoverService.testRetry();
            System.out.println(str);

        }); 
    }
}

这是Spring Boot应用程序类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableRetry
@EnableAsync
@SpringBootApplication
public class RetryExampleApplication {


    @Autowired
    RetryAndRecoverService retryAndRecoverService;

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

    private void retryAndRecoverService() {
        retryAndRecoverService.retryWithException();

    }
}

例外情况如下。对此有任何建议。

retryWithException - 0

2018-04-05 12:45:17.644 ERROR 23052 --- [cTaskExecutor-1] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected error occurred invoking async method 'public java.lang.String com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry()'.

com.mscharhag.springretrydemo.RetryAndRecoverService$FooException: null
    at com.mscharhag.springretrydemo.RetryAndRecoverService.retryWithException(RetryAndRecoverService.java:19) ~[classes/:na]
    at com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry(RetryAndRecoverService.java:47) ~[classes/:na]
    at com.mscharhag.springretrydemo.RetryAndRecoverService$$FastClassBySpringCGLIB$$f31442b9.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:121) ~[spring-retry-1.1.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_141]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_141]

1 个答案:

答案 0 :(得分:0)

情况与此类似,并在春季版本中得到修复 4.3.14请参阅https://jira.spring.io/browse/SPR-16196