Java Apache PoolingHttpClientConnectionManager 抛出非法状态异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25889925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache PoolingHttpClientConnectionManager throwing illegal state exception
提问by abipc
Here is how I use it -
这是我如何使用它 -
private static final PoolingHttpClientConnectionManager connPool;
static {
connPool = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
connPool.setMaxTotal(200);//configurable through app.properties
// Increase default max connection per route to 50
connPool.setDefaultMaxPerRoute(20);//configurable through app.properties
}
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connPool) .build();
ALso I have put a finally block around http GET -
另外我在 http GET 周围放置了一个 finally 块 -
finally {
try {
httpClient.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
Here is my stacktrace -
这是我的堆栈跟踪 -
java.lang.IllegalStateException: Connection pool shut down
at org.apache.http.util.Asserts.check(Asserts.java:34)
at org.apache.http.pool.AbstractConnPool.lease(AbstractConnPool.java:169)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:217)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:157)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at com.A.B.C.CustomHttpClient.doGETAndValidate(CustomHttpClient.java:44)
at com.A.B.C.SiteMonitorTask.monitorAndUpdateEndPoints(SiteMonitorTask.java:48)
at com.A.B.C.SiteMonitorTask.run(SiteMonitorTask.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
I am using Quartz to schedule a job of monitoring Http end points.. Here is my connection pool configuration
我正在使用 Quartz 来安排监控 Http 端点的工作。这是我的连接池配置
totalMaxHttpConn=200
maxHttpConnPerRoute=20
Maven dependency.. artifact version
Maven 依赖.. 工件版本
httpclient 4.3.1
httpcore 4.3.1
EDIT- Well the problem got away by not closing CloseableHttpClient in the finally block.. Can anyone tell why is it behaving like that? Why is connection pool shut down if i close a client?
编辑- 好吧,问题是通过在 finally 块中不关闭 CloseableHttpClient 来解决的。谁能告诉它为什么会这样?如果我关闭客户端,为什么连接池会关闭?
Is the closeablehttpclient above is a handle to the pool rather than a single conn
上面的closeablehttpclient是池句柄而不是单个conn
采纳答案by ok2c
This behavior is due to a bug in HC 4.3. It has already been fixed in HC 4.4a1. As of 4.4 CloseableHttpClient#close
should automatically shut down the connection pool only if exclusively owned by the client
此行为是由于 HC 4.3 中的错误造成的。它已在 HC 4.4a1 中修复。从 4.4 开始,CloseableHttpClient#close
只有在客户端独占时才应自动关闭连接池
回答by Roberg
In version 4.4 the method setConnectionManagerShared was added to HttpClientBuilder. If you set it to true the client won't close the connection manager.
在 4.4 版本中,方法 setConnectionManagerShared 被添加到 HttpClientBuilder。如果您将其设置为 true,客户端将不会关闭连接管理器。
HttpClients.custom()
.setConnectionManager(Util.getConnectionManager()) // shared connection manager
.setConnectionManagerShared(true)