gpt4 book ai didi

java.lang.IllegalStateException : Connection pool shut down

转载 作者:行者123 更新时间:2023-12-01 08:45:39 26 4
gpt4 key购买 nike

我正在尝试使用 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)

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

任何帮助将不胜感激!

最佳答案

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

关于java.lang.IllegalStateException : Connection pool shut down,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com