【问题标题】:java.lang.IllegalStateException: Connection pool shut down exceptionjava.lang.IllegalStateException:连接池关闭异常
【发布时间】:2019-11-25 21:58:27
【问题描述】:
我已将我的代码版本从 http 更改为 https,并且我使用 HttpClient client = HttpClientFactory.getHttpsClient() 进行执行。当我第一次尝试运行我的代码时,它运行良好,下次抛出异常
java.lang.IllegalStateException: 连接池关闭异常
我正在使用 4.5HC。
【问题讨论】:
标签:
java
httpurlconnection
connection-pooling
【解决方案1】:
如果您正在合并连接,请不要在请求后关闭您的客户端。
也就是说,你可能正在做这样的事情:
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() 导致您的下一个池连接失败。