title: Apache HttpClient Read timed out after a while
body: <p>I've scenario where I use Spring Boot + RestTemplate + Apache HttpClient to fetch information. In my current set-up I'm running the requests in 5 threads. But after few minutes, the connections start to time out.</p>
  
  <p>When I restart the the Spring Boot app, the requests are fine again for a few minutes. Not sure what causes these timeouts.</p>
  
  <pre><code>2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.h.client.protocol.RequestAuthCache   : Auth cache not set in the context
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {s}-&gt;https://www.somewebsite.com:443][total available: 5; route allocated: 1 of 20; total allocated: 5 of 20]
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 29][route: {s}-&gt;https://www.somewebsite.com:443][total available: 4; route allocated: 1 of 20; total allocated: 5 of 20]
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: set socket timeout to 0
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: set socket timeout to 10000
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Executing request GET &lt;url&gt; HTTP/1.1
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Target auth state: UNCHALLENGED
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; GET &lt;url&gt; HTTP/1.1
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; User-Agent: Mozilla/5.0
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; Accept: text/plain, application/json, application/*+json, */*
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; Host: www.somewebsite.com
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; Connection: Keep-Alive
  2020-03-21 12:52:24.195 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; Cookie: &lt;Cookies&gt;
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.headers                  : http-outgoing-29 &gt;&gt; Accept-Encoding: gzip,deflate
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "GET &lt;url&gt; HTTP/1.1[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "User-Agent: Mozilla/5.0[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "Accept: text/plain, application/json, application/*+json, */*[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "Host: www.somewebsite.com[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "Connection: Keep-Alive[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "Cookie: &lt;cookies&gt; [\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "Accept-Encoding: gzip,deflate[\r][\n]"
  2020-03-21 12:52:24.196 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &gt;&gt; "[\r][\n]"
  2020-03-21 12:52:34.197 DEBUG 72712 --- [        task-14] org.apache.http.wire                     : http-outgoing-29 &lt;&lt; "[read] I/O error: Read timed out"
  2020-03-21 12:52:34.198 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: Close connection
  2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-29: Shutdown connection
  2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] o.a.http.impl.execchain.MainClientExec   : Connection discarded
  2020-03-21 12:52:34.199 DEBUG 72712 --- [        task-14] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 29][route: {s}-&gt;https://www.somewebsite.com:443][total available: 4; route allocated: 0 of 20; total allocated: 4 of 20]
  </code></pre>
  
  <p>This is my setup</p>
  
  <pre><code>  @Bean
      public ClientHttpRequestFactory createRequestFactory(final CookieStore cookieStore) {
          final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
          connectionManager.setMaxTotal(20);
          connectionManager.setDefaultMaxPerRoute(20);
          final RequestConfig requestConfig = RequestConfig
                  .custom()
                  .setCookieSpec(CookieSpecs.STANDARD)
                  .setConnectionRequestTimeout(5000)
                  .setSocketTimeout(10000)
                  .build();
  
          final CloseableHttpClient httpClient = HttpClients
                  .custom()
                  .setConnectionManager(connectionManager)
                  .setDefaultRequestConfig(requestConfig)
                  .setDefaultCookieStore(cookieStore)
                  .build();
          return new HttpComponentsClientHttpRequestFactory(httpClient);
      }
  
      @Bean
      public RestTemplate restTemplate(final ClientHttpRequestFactory clientHttpRequestFactory) {
          final DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(
                  "&lt;url&gt;");
          uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
  
          final RestTemplate restTemplate = new RestTemplateBuilder()
                  .uriTemplateHandler(uriBuilderFactory)
                  .defaultHeader("User-Agent", "Mozilla/5.0")
                  .build(RestTemplate.class);
  
          restTemplate.setRequestFactory(clientHttpRequestFactory);
          return restTemplate;
      }
  </code></pre>
  
  <p>Even without reusing connections</p>
  
  <pre><code>HttpClients
                  .custom()
                  .setConnectionReuseStrategy(new NoConnectionReuseStrategy())...
  </code></pre>
  
  <p>I still encounter the problem. Maybe someone has encountered something similar or have any ideas?</p>