Search code examples
springtimeoutconnectionhttpclientresttemplate

RestTemplate HttpClient connectionRequestTimeout


In order to configure RestTemplate I use the following configuration:

HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
httpRequestFactory.setConnectTimeout(connectionTimeoutMs);
httpRequestFactory.setConnectionRequestTimeout(readTimeoutMs);
httpRequestFactory.setReadTimeout(readTimeoutMs);

RestTemplate restTemplate = new RestTemplate(httpRequestFactory);

I understand the purpose of connection and read timeouts. But I don't understand the purpose of connection request timeout. It also not clear from Javadoc what does it mean. Could you please explain it ?


Solution

  • As the docs say :

    Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying HttpClient.

    It means the maximum amount of time you will allow to the connection manager to give you an available connection from its pool (so it has nothing to do with the RESTservice itself you'll reach).

    To define a custom connection manager, you can use this :

    CloseableHttpClient httpClientBuilder = HttpClientBuilder.create().setConnectionManager(...).build();
    HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClientBuilder);