我正在编写一个客户端-服务器应用程序。我在客户端使用CloseableHttpClient
发送GET请求。我想向服务器发送5个GET请求。然而,只有我在前两个请求中得到了响应。下面是我的客户端代码:
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class Test {
public static void main(String[] args) {
HttpGet getreq = new HttpGet("http://shalakha:8089/Gateway/ReverseInvokeListener");
CloseableHttpClient c1 = HttpClients.createMinimal();
try {
for(int i=0;i<5;i++){
CloseableHttpResponse resp = c1.execute(getreq);
System.out.println(resp);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面是我得到的回应:
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Set-Cookie: JSESSIONID=0D4ABDC362FEC3030A5EE3709F3096FD; Path=/Gateway, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:22 GMT]
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:26 GMT]
其中'CLIENTREQ‘是我在发送响应时从服务器端添加的自定义标头。
有什么可以说明这是什么原因的吗?server.xml
中的connectionTimeout
设置为'-1‘,表示无限超时。
发布于 2015-04-09 18:00:21
您的代码已经泄漏了所有可用的连接(默认情况下是两个),并且耗尽了连接池。您需要关闭响应对象,以确保连接释放回池。
http://hc.apache.org/httpcomponents-client-4.4.x/tutorial/html/fundamentals.html#d5e145
https://stackoverflow.com/questions/29534990
复制相似问题