spring-boot - Spring Retry 不再调用 maxAttempts
问题描述
try {
reponseType = retryTemplate.execute((RetryCallback<X, RetryException>) context -> {
try {
log.error("Calling api attempt #" + context.getRetryCount());
HttpEntity<x> xResponse = httpRestTemplate.exchange(requestUrl, HttpMethod.POST, entity, x.class);
return xResponse.getBody();
} catch (HttpStatusCodeException e) {
if (e.getStatusCode().is5xxServerError()) {
throw new RetryException("api returned Server Error", e);
}
return null;
}
});
} catch (RetryException e) {
throw e;
配置文件中定义了retryTemplate策略,httpRestTemplate为普通模板
@Bean
RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(30000);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
retryTemplate.setRetryPolicy(new CustomRetryPolicy(3));
return retryTemplate;
}
但在给定时间后无法再次调用 retryTemplate。谁能帮助我,我想我在异常游戏中感到困惑。
解决方案
这只会在e.getStatusCode().is5xxServerError()
. 否则,您将返回null
,从重试模板的角度来看,这是“成功”。模板只会在抛出异常时重试。
您可以在重试策略中对哪些异常可重试进行分类。
推荐阅读
- javascript - Fetch API 通过 .map() 抓取数据并返回结果
- java - 为什么显示的数字高于java中的输入数字?
- symfony4 - Symfony 4 中的 Liipimaginebundle
- ruby-on-rails - 将字符串转换为数组并进行操作
- ios - Xamarin.Forms Button ContentLayout 属性未按预期工作
- javascript - 如何通过 socket.io 发送数组
- mysql - Doctrine - 具有自定义主键的多对多
- excel - Excel:当另一列中的值以 R 开头时,计算唯一/非重复值
- ios - 无法使用 Parse 在滚动视图中缩放 UIImage
- hive - Hive UDF 返回多列输出