小编Sab*_*h.K的帖子

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

下面的@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();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是 Junit 测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RetryExampleApplication.class)
public class RetryTest { …
Run Code Online (Sandbox Code Playgroud)

spring dynamic-proxy async-await spring-retry spring-boot

5
推荐指数
1
解决办法
4142
查看次数