问题引入
现有一个接口,调用4次后才可已返回正常结果
public class RetryService {
private AtomicLong times = new AtomicLong();
public String hello () {
long l = times.incrementAndGet();
if (l % 4 == 0) {
throw new RuntimeException();
}
return "hello world";
}
}
解决方案
方式一: 硬核捕获
public void method1 () {
for(int i = 0;i< 4;i++) {
try{
retryService.hello();
} catch (Exception) {
log.error("接口请求失败!");
}
}
throw new RuntimeException("处理失败");
}