0

In my spring-boot (2.4.0) app I have set up a connection pool and the timeout for outgoing HTTP requests (30 seconds):

@Bean
public RequestConfig requestConfig() {
    return RequestConfig.custom()
            .setConnectionRequestTimeout(30000)
            .setConnectTimeout(30000)
            .setSocketTimeout(30000)
            .build();
}

@Bean
public CloseableHttpClient httpClient(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager,
                                      RequestConfig requestConfig) {
    return HttpClientBuilder
            .create()
            .setConnectionManager(poolingHttpClientConnectionManager)
            .setDefaultRequestConfig(requestConfig)
            .build();
}

@Bean
public RestTemplate restTemplate(HttpClient httpClient) {
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);
    return new RestTemplate(requestFactory);
}

I autowire restTemplate bean in my Gateway classes and use this restTemplate.exchange(...) to perform HTTP requests. The timeout itself works fine and is applied to all outgoing requests.

But for certain URLs I need to set the timeout to 5 seconds.

Is there a way to override generic timeout settings for certain URLs?

htshame
  • 6,599
  • 5
  • 36
  • 56

1 Answers1

0

I had this very this problem recently and had two versions of RestTemplate, one for "short timeout" and one for "long timeout". See here.

RestTemplate was really designed to be built with pre-configured timeouts and for those timeouts to stay untouched after initialization. If you use Apache HttpClient then yes you can set a RequestConfig per request and that is the proper design in my opinion.

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29