java.lang.IllegalStateException:连接池关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44130464/
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
java.lang.IllegalStateException: Connection pool shut down
提问by User3
I am trying to post data to a REST service using Http and I have configured my client as follows:
我正在尝试使用 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;
Now I have an executor service which is responsible for calling the actual posting, I hand over the above params to Executor service as follows:
现在我有一个 executor 服务负责调用实际发布,我将上述参数交给 Executor 服务,如下所示:
executor.execute(new LocalThreadPoolExecutor(line, client, httpPost,response ));
line is the JSON payload I am trying to send across.
line 是我尝试发送的 JSON 有效负载。
Now my method in executor service looks like:
现在我在执行程序服务中的方法如下所示:
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();
}
And and I am getting an exception at response = client.execute(httpPost);
而且我在 response = client.execute(httpPost);
Which is:
这是:
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)
I know where the problem is, however I am unable to determine how to solve it!
我知道问题出在哪里,但我无法确定如何解决!
Any help would be highly appreciated!
任何帮助将不胜感激!
回答by Sivabalakrishnan
client.close();will close the connection of CloseableHttpClient. If you access the execute method on the second time after the connection gets closed, This error will throw.
客户端关闭();将关闭 CloseableHttpClient 的连接。如果在连接关闭后第二次访问 execute 方法,则会抛出此错误。