java.lang.IllegalStateException:连接池关闭异常

时间:2019-02-04 10:11:57

标签: java httpurlconnection connection-pooling

我已将代码版本从http更改为https,并且出于执行目的,我使用HttpClient client = HttpClientFactory.getHttpsClient()。当我第一次尝试运行我的代码时,它运行良好,而下一次则抛出异常

  

java.lang.IllegalStateException:连接池关闭异常

我正在使用4.5HC。

1 个答案:

答案 0 :(得分:0)

如果您正在合并连接,请不要在请求后关闭客户端。

也就是说,您可能正在执行以下操作:

PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
...
CloseableHttpClient httpclient = HttpClients.custom()
     .setConnectionManager(pool)
     .build();

try { // try-with-resources
    HttpGet httpget = new HttpGet(url.toURI());
    try (CloseableHttpResponse response = httpclient.execute(httpget);
             InputStream fis = response.getEntity().getContent();
            ReadableByteChannel channel = Channels.newChannel(fis)) {
             // ... get data ...
     } finally {
         httpclient.close(); <====== !!
     }
} catch (IOException | URISyntaxException e) {
    // exception handling ...
}

httpclient.close()导致您的下一个池连接失败。