【发布时间】:2015-01-29 22:05:09
【问题描述】:
是否可以根据错误状态码在spring retry(https://github.com/spring-projects/spring-retry)中设置RetryPolicy?例如我想用HttpStatus.INTERNAL_SERVER_ERROR 状态码重试HttpServerErrorException,即503。因此它应该忽略所有其他错误代码——[500 - 502] 和[504 - 511]。
【问题讨论】:
-
不是直接的,但是如果您可以提供更多有关如何调用服务器的上下文(例如,如果您使用 Spring Integration 出站网关,或者您是否直接从您的代码),我们也许可以提出解决方案。
-
我扩展了 RestTemplate 并用 RetryTemplate 覆盖了一些围绕它们的方法。我正在关注上面 github 链接上给出的示例,类似于... SimpleRetryPolicy policy = new SimpleRetryPolicy(); policy.setMaxAttempts(5); policy.setRetryableExceptions(new Class[] {HttpServerErrorException.class}); Spring restTemplate 报告 http 状态错误代码 500 - 511 的“HttpServerErrorException.class”,但我想在 503 和 504 上重试。
-
现在,我在 doWithRetry(RetryContext context) 中从 RetryContext 中提取 throwable 并读取错误消息 - context.getLastThrowable().getMessage(),然后查找 503 或 504。必须我认为这是一种更好的方法。
-
@MaratKurbanov,你能帮我们做同样的事情吗,根据特定的http状态码重试
-
@GaryRussell 我正在使用 Spring Integration 出站网关来调用服务器。你能提出一个解决方案吗?
标签: spring spring-retry