【问题标题】:Retry in RestTemplate not working against ResourceAccessException在 RestTemplate 中重试不适用于 ResourceAccessException
【发布时间】:2022-10-13 20:41:47
【问题描述】:

我的重试处理程序不适用于 ResourceAccessException。这仅适用于 IOException 及其子类型。我什至尝试添加拦截器但没有运气。知道如何为 ResourceAccessException 添加重试吗???

@Bean
public ClientHttpRequestFactory clientFactory() {
    HttpClient httpClient = HttpClients.custom()            
        .setRetryHandler((exception, executionCount, context) -> {
            if (executionCount > 3) {
                log.warn("Maximum retries {} reached", 3);
                return false;
            }
            if (<some condition for retry>) {
                log.warn("Retry {}", executionCount);
                return true;
            }
            return false;
        })
        .build();

    return new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Bean
public RestTemplate customRestTemplate(@Qualifier("clientFactory") ClientHttpRequestFactory clientFactory){ 
    return new RestTemplate(clientFactory);
}

【问题讨论】:

    标签: java spring spring-boot httpclient resttemplate


    【解决方案1】:

    ResourceAccessException 通常包装来自 Spring RestTemplate 的 IOException,其中 HttpClient 不是来自 Spring。

    除了 HTTP 503 Service Unavailable 状态响应等情况外,这只是可能重试的情况之一。

    基于 RestTemplate/HttpClient 同时结合所有案例的解决方案可能会很棘手。

    要为 ResourceAccessException 添加重试,相关主题可能会有所帮助:
    Solution based on Spring RetryTemplate

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2017-12-31
      • 2020-07-07
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多