spring - Springboot中如何根据HTTP响应码进行重试
问题描述
@Retryable(value = ABCDException.class,
maxAttemptsExpression = 3,
backoff = @Backoff(delayExpression = "#{${application.delay}}"))
public String postABCDrequest(ABCDrequest abcdRequest) throws ABCDException {
try {
return restCalltopostData(abcdRequest);
} catch (AnyException e) {
log.error("Error Occured ", e);
throw new ABCDException("Error Occured ", e);
}
}
在这种方法中,只有当我得到某些响应代码时,我才需要重试发布数据。我搜索了一些不适合我的解决方案的选项。使用注释有没有更简单的方法?
解决方案
在 catch 块中,您将无法获取响应代码。由于您对 all 感兴趣,如果异常在服务器端得到很好的处理并返回状态码5xx
,请检查response.getStatusCode().is5xxServerError()
并重新抛出异常。ABCDException.class
这样,您的代码将继续重试所有5xx
异常,直到maxAttempts
耗尽。
否则,您可以HttpServerErrorException.class
通过替换来简单地重试ABCDException.class
.
推荐阅读
- mysql - MySQL - 如何修改复杂的内部连接和连接查询?
- wsdl - 如何使 MuleSoft webconsumer 连接器字段获取动态值
- python - 带有 eventlet 的 Flask-SocketIO:Web 和 Websockets 线程
- python - Groupby 并根据 Pandas 中的另一列对一列进行降序排列
- auth0 - 赛普拉斯无法使用新的 auth0-spa-js 包请求 API 或显示内容
- macros - 如何在 sbcl common lisp 的 let 绑定中定义(defmacro)宏?
- c - 如何在没有 GPU 的情况下在 Arm 上加速 Gtk2+ 的 Cairo 绘画速度
- windows - 如何在 .bat 文件的“for”命令中格式化 Tesseract-OCR 语言设置
- java - 如何通过摆脱冗余来优化下面的代码?
- twitter-bootstrap - 导航切换向下滚动超过该部分的开头