当前位置: 首页 > 知识库问答 >
问题:

Java语言lang.IllegalState异常:连接池关闭

拓拔霄
2023-03-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 service负责调用实际发布,我将上述参数移交给executor service,如下所示:

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

line是我试图发送的JSON负载。

现在,我在executor服务中的方法如下所示:

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上遇到了一个异常。执行(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)

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

任何帮助都将不胜感激!


共有1个答案

经兴安
2023-03-14

客户关闭();将关闭CloseableHttpClient的连接。如果在连接关闭后第二次访问execute方法,则会引发此错误。

 类似资料:
  • 当从S3下载文件时,它失败了,给了我这个异常 Java语言lang.IllegalState异常:连接池关闭 com.amazonaws.http.conn.Aandler.invoke(Aactory.java:76) 在com.amazonaws.http.conn.AbstractConnProxy70.request(AbstractConnPorg.apache.http.impl.ex

  • 我已经将我的代码版本从http更改为https,我正在使用HttpClient=HttpClientFactory。getHttpsClient(),用于执行目的。当我第一次尝试运行代码时,它运行良好,下次抛出异常 Java语言lang.IllegalStateException:连接池关闭异常 我使用的是4.5HC。

  • 我正在Java中运行Apache HTTP POST请求,每当我超过1300个请求时,就会出现以下错误,我需要发出更多的请求,大约40k,我可以做些什么来纠正相同的错误?

  • 我正在使用Hibernate 3.3和Oracle 11g开发Struts 2 Framework。我的Web项目自5个月以来一直运行良好。但是最近我面临着java.sql.SQLRecoverableException:在一些空闲时间关闭连接。我将解释以下场景...我的hibernate.cfg.xml配置是 我的HibernateSessionFactory配置是 然后在运行以下代码时出错 当

  • 我希望在我的Spring 4应用程序中使用Hikari连接池。数据库是Google CloudSQLPostgres数据库。 我在pom中有以下依赖项。xml: 在我的申请Context.xml,我有: 但是我得到了以下例外: 出了什么问题?

  • 问题内容: 我们使用JDBC的标准代码部分是… 问题1:使用连接池时,是否应该在最后关闭连接?如果是这样,合并的目的就不会丢失吗?如果不是,那么DataSource如何知道何时释放Connection的特定实例并可以重用?我对此感到有些困惑,任何指针都表示赞赏。 问题2:以下方法是否接近标准?看起来像是尝试从池中获取连接,并且如果无法建立DataSource,请使用老式的DriverManager