1

My retry handler not working against ResourceAccessException. This only works against IOException and its sub-type. I even tried adding interceptor but no luck. Any idea how to add retry for 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);
}
user2001165
  • 135
  • 1
  • 1
  • 3

1 Answers1

0

ResourceAccessException is commonly wrapping IOException coming from Spring RestTemplate, where HttpClient is not from Spring.

This is only one of the cases to be possibly retried, in addition to cases like HTTP 503 Service Unavailable status response.

Solution based on RestTemplate/HttpClient while combining all cases can be tricky.

To add retry for ResourceAccessException, related topic may help:
Solution based on Spring RetryTemplate

Liran
  • 144
  • 2
  • 6