【问题标题】:java.lang.IllegalStateException: Connection pool shut downjava.lang.IllegalStateException:连接池关闭
【发布时间】:2017-10-23 03:36:14
【问题描述】:

我正在尝试使用 Http 将数据发布到 REST 服务,我的客户端配置如下:

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(60);
        cm.setDefaultMaxPerRoute(60);
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        CloseableHttpResponse response = null;

现在我有一个执行器服务负责调用实际发布,我将上述参数交给执行器服务如下:

executor.execute(new LocalThreadPoolExecutor(line, client, httpPost,response ));

line 是我尝试发送的 JSON 有效负载。

现在我在执行器服务中的方法如下:

private void postData(String data, CloseableHttpClient client, HttpPost httpPost,
                         CloseableHttpResponse response) throws Exception {
        System.out.println("Post hit");
        StringEntity entity = new StringEntity(data);
        httpPost.setEntity(entity);
        response = client.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();
        System.out.println("Stat" + code);

        logger.info("Response Code: " + code);

        String temp;
        String builder = "Response: ";
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        while ((temp = bufferedReader.readLine()) != null) {
            builder = builder + (temp + "\n");
        }

        System.out.println(builder);
        //logger.info(payload);
        client.close();
    }

而且我在response = client.execute(httpPost); 遇到了一个异常

即:

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:189)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:257)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:176)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at consumer.ril.com.LocalThreadPoolExecutor.postData(LocalThreadPoolExecutor.java:80)
    at consumer.ril.com.LocalThreadPoolExecutor.run(LocalThreadPoolExecutor.java:40)
    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:745)
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:189)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection(PoolingHttpClientConnectionManager.java:257)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:176)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at consumer.ril.com.LocalThreadPoolExecutor.postData(LocalThreadPoolExecutor.java:80)
    at consumer.ril.com.LocalThreadPoolExecutor.run(LocalThreadPoolExecutor.java:40)
    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:745)

我知道问题出在哪里,但是我无法确定如何解决它!

任何帮助将不胜感激!

【问题讨论】:

  • 尽量避免使用HttpClient 4.3 你用的是哪一个?
  • 使用 4.5 org.apache.httpcomponentshttpclient4.5.3
  • 不是Executor服务的问题,已经是init了。这里的错误不是通用的。我有一个指向问题的堆栈跟踪。也许你可以帮助我了解更多?
  • 我在方法结束时发现了问题:client.close();。这是一个复制粘贴错误!还将 CloseableHttpClient 更改为简单的 HttpClient。
  • 你好 User3,你能发布你是如何解决这个问题的,我也面临这个问题

标签: java apache-httpclient-4.x


【解决方案1】:

client.close(); 会关闭 CloseableHttpClient 的连接。如果您在连接关闭后第二次访问执行方法,则会抛出此错误。

【讨论】:

  • 谢谢!! ...这对解决此错误很有帮助,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多