5

We are getting connection pool shutdown error while executing http requests using RestTemplate.

This is not a frequent error. It happened around 3 times in a month for a very short duration (less than a minute).

We are using PoolingHttpClientConnectionManager with 50 max connections and connectTimeout=60sec

  @Bean
  public RestTemplate restTemplate()
  {
    final RestTemplate restTemplate = new RestTemplate ( httpRequestFactory () );
    return restTemplate;
  }

  @Bean
  public ClientHttpRequestFactory httpRequestFactory()
  {
    return new HttpComponentsClientHttpRequestFactory ( httpClient () );
  }

  @Bean
  public CloseableHttpClient httpClient()
  {
    final SSLContext sslcontext = SSLContexts.createSystemDefault ();
    final X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier ();
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
      .<ConnectionSocketFactory> create ()
      .register ( "http", PlainConnectionSocketFactory.INSTANCE )
      .register ( "https", new SSLConnectionSocketFactory ( sslcontext, hostnameVerifier ) )
      .build ();

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager (
      socketFactoryRegistry );
    connectionManager.setMaxTotal ( 50 );
    final RequestConfig config = RequestConfig.custom ()
      .setConnectTimeout ( 60000 ).build ();

    final CloseableHttpClient defaultHttpClient = HttpClientBuilder
      .create ().setConnectionManager ( connectionManager )
      .setDefaultRequestConfig ( config ).build ();

    LOGGER.info ( "Initializing CloseableHttpClient" );
    return defaultHttpClient;
  }

spring-webmvc version 4.0.6.RELEASE;
httpclient version 4.3.5

The exact stack trace is as follows...

java.lang.IllegalStateException: Connection pool shut down
        at org.apache.http.util.Asserts.check(Asserts.java:34) ~[httpcore-4.3.2.jar:4.3.2]
        at org.apache.http.pool.AbstractConnPool.lease(AbstractConnPool.java:169) ~[httpcore-4.3.2.jar:4.3.2]
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:221) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:158) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.3.5.jar:4.3.5]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.3.5.jar:4.3.5]
        at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:87) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:545) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:243) ~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
1

1 Answer 1

18

try to change your code like this:

HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(true).build();

the setConnectionManagerShared will defines the connection manager is to be shared by multiple client instances.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.