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

问题描述 投票:1回答:1

下面的@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课程

@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]
spring spring-boot
1个回答
0
投票

类似的问题是这个案例,并修复了弹簧版本4.3.14请参阅https://jira.spring.io/browse/SPR-16196

© www.soinside.com 2019 - 2024. All rights reserved.