title: HttpClient Connection pool shut down
body: <p>I am using HttpClient v4.5.5</p>
  
  <p>I have an <code>HttpClient</code> as below:</p>
  
  <pre><code>PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
      connManager.setMaxTotal(totalMaxConnections);
      connManager.setDefaultMaxPerRoute(defaultMaxConnPerRoute);
  CloseableHttpClient httpClient =HttpClients.custom().setConnectionManager(connManager).setConnectionManagerShared(true).build();
  </code></pre>
  
  <p>Then I use the http client as below:</p>
  
  <pre><code>protected Response connect(final Function&lt;AbstractHttpAdapter, CloseableHttpResponse&gt; pcAction) {
      Response response = null;
      final Instant begin = Instant.now();
      try {
          final CloseableHttpResponse closableResp = pcAction.apply(this);
          try {
              final Instant end = Instant.now();
              if (closableResp != null) {
                  final HttpEntity responseEntity = closableResp.getEntity();
                  if (responseEntity != null) {
                      response = new Response();
                      InputStream is = responseEntity.getContent();
                      try {
  
                          final ContentType contentType = ContentType.getOrDefault(responseEntity);
                          Charset charset = contentType.getCharset();
                          if (charset == null)
                              charset = Charset.forName("UTF-8");
                          response.responseText = IOUtils.toString(is, charset.toString());
                          if (closableResp.getStatusLine() != null) {
                              response.statusLine = closableResp.getStatusLine();
  
                          }
                          Map&lt;String, String&gt; responseHeaders = new HashMap&lt;&gt;();
                          Header[] headers = closableResp.getAllHeaders();
                          for (Header h : headers) {
                              responseHeaders.put(h.getName(), h.getValue());
                          }
                          response.responseHeaders = responseHeaders;
                          response.responseDuration = Duration.between(begin, end).toMillis();
                      } catch (UnsupportedOperationException | IOException e) {
                          LOGGER.error("IO Error: [{}]", e.getMessage());
                          LOGGER.debug("IO Error: {}", e);
                          return null;
                      } finally {
                          is.close();
                      }
                  }
              } else {
                  LOGGER.debug("NULL CloseableHttpResponse!");
              }
          } finally {
              if (closableResp != null)
                  closableResp.close();
          }
      } catch (IOException e) {
          LOGGER.error("IO Error: [{}]", e.getMessage());
          LOGGER.debug("IO Error: {}", e);
          response = null;
      } catch (Exception ex) {
          LOGGER.error("IO Error: [{}]", ex.getMessage());
          LOGGER.debug("IO Error: {}", ex);
          response = null;
      }
      return response;
  }
  
  public CloseableHttpResponse executePost(final URL url, final String request, final int connectTimeout,
          final int readTimeout, Map&lt;String, String&gt; extraHeaders) {
  
      LOGGER.trace("Executing post request...");
      CloseableHttpResponse closeableHttpResponse = null;
      final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
      final RequestConfig requestConfig = requestConfigBuilder.setSocketTimeout(readTimeout)
              .setConnectTimeout(connectTimeout).build();
  
      final URI uri = prepareUri(url, null);
      final HttpPost httpPost = new HttpPost(uri);
      try {
          httpPost.setEntity(new StringEntity(request, StandardCharsets.UTF_8));
          httpPost.setConfig(requestConfig);
          if (MapUtils.isNotEmpty(extraHeaders)) {
              for (Map.Entry&lt;String, String&gt; header : extraHeaders.entrySet()) {
                  httpPost.setHeader(header.getKey(), header.getValue());
              }
          }
          closeableHttpResponse = httpClient.execute(httpPost, HttpClientContext.create());
      } catch (ClientProtocolException e) {
          LOGGER.error("HTTP Error for URL [{}] and RequestParams [{}]: {}", url, request, e);
      } catch (IOException e) {
          LOGGER.error("IO Error for URL [{}] and RequestParams [{}]: {}", url, request, e.getMessage());
          LOGGER.debug("IO Error for URL [{}] and RequestParams [{}]: {}", url, request, e);
      } catch (Exception e) {
          LOGGER.error("General Error for URL [{}] and RequestParams [{}]: {}", url, request, e);
      }
      return closeableHttpResponse;
  }
  </code></pre>
  
  <p>calling <code>connect</code> via <code>threadPoolTaskScheduler</code> periodically (every few minutes)</p>
  
  <p>Once in a while I get an error </p>
  
  <p><code>java.lang.IllegalStateException: Connection pool shut down</code> and from what I have read this happens either with older HttpClient version or when you close the HttpClient. Which I do not do. So I cannot understand why I get this error. It recovers but it is a problem having exceptions like that.</p>