title: Default httpclient for resttemplate, have absolute value for read timeout
body: <p>I have a problem where a application keeps blocking indefinitely on a post call made with a <code>RestTemplate</code> from Spring Boot.</p>
  
  <pre class="lang-java prettyprint-override"><code>ResponseEntity&lt;String&gt; response = restTemplate.postForEntity(destination.getUri(), request, String.class);
  </code></pre>
  
  <p>We use the default standard JDK implementation and create it like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>    this.restTemplate = restTemplateBuilder
                         .setConnectTimeout(5000)
                         .setReadTimeout(5000)
                         .build();
  </code></pre>
  
  <p>Which sets the connection and read timeout to 5 seconds. But it seems this is not an absolute value, as soon as our application receives some bytes this read timeout resets and this causes our application to wait indefinitely. </p>
  
  <p>I rather have an absolute read timeout where if you don't get the end response in less than 5 seconds the template throws an <code>TimeoutException</code>.          </p>
  
  <p>I couldn't find something like this in the options for the default client?</p>
  
  <p>---EDIT---</p>
  
  <p>I tried out @Peekay answer but it doesn't seem to work:</p>
  
  <pre><code>    CloseableHttpClient httpClient = HttpClientBuilder.create()
             .setConnectionTimeToLive(1, TimeUnit.SECONDS)
             .setConnectionManager(new PoolingHttpClientConnectionManager())
             .build();
  
     HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
     clientHttpRequestFactory.setHttpClient(httpClient);
     return new RestTemplate(clientHttpRequestFactory);
  </code></pre>
  
  <p>I have also tried different implementations of the client's <code>RestTemplate</code> e.g. <code>HttpComponentsClientHttp</code>, <code>Netty4Client</code> and <code>OkHttp3Client</code> created them like so:</p>
  
  <pre><code>    Netty4ClientHttpRequestFactory factory = new Netty4ClientHttpRequestFactory();
      factory.setConnectTimeout(timeout);
      factory.setReadTimeout(readTimeout);
      return new RestTemplate(factory);
  </code></pre>
  
  <p>And tested them on a response that took longer than 5 seconds to respond. All of them except for Netty, which returned a <code>ReadTimeoutException</code>, had returned a 200 success. Unfortunately I cannot switch to that client, it seems you need to implement it yourself if you want to keep using the default client. </p>