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