这是我的用法-

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();

另外,我在http GET周围放置了一个finally块-
finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
            }
        }

这是我的堆栈跟踪-
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)

我正在使用Quartz计划监视Http端点的工作。这是我的连接池配置
totalMaxHttpConn=200
maxHttpConnPerRoute=20

Maven依赖..工件版本
httpclient 4.3.1
httpcore 4.3.1

编辑-好吧,通过不关闭finally块中的CloseableHttpClient,问题解决了。有人能说出为什么这样吗? 如果关闭客户端,为什么连接池会关闭?

是上面的closeablehttpclient是池的句柄,而不是单个conn

最佳答案

此行为是由于HC 4.3中的错误所致。它已在HC 4.4a1中修复。从4.4版本开始,CloseableHttpClient#close仅在客户端独占时才应自动关闭连接池。

关于java - Apache PoolingHttpClientConnectionManager引发非法状态异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25889925/

10-08 22:24