我们有一个HttpClient(apache.DefaultHttpClient)对象池(GenericObjectPool)。通过这些客户端将HttpPost对象放入执行,可以同时发送Post请求。使用的协议是: HTTP/1.1 with keepalive。
在通过netstat进行负载测试期间观察到,新的套接字连接被不加区别地创建,而旧的连接转到TIME_WAIT。
日志摘录:
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "POST /INBOX/4504a09e-13c0-3853-a285-9e2b9a22f65e/1e1e5a20-a8c1-11e2-99b8-7c19e9129271 HTTP/1.1[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "Content-Type: application/json; charset=UTF-8[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "Content-Length: 117[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "Host: rwcdtgxb0402:15010[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "Connection: Keep-Alive[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "User-Agent: Apache-HttpClient/4.2.1 (java 1.5)[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - >> "[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - << "HTTP/1.1 200 OK[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - << "Content-Length: 0[\r][\n]"
[Worker-2] org.apache.http.impl.conn.Wire 63 - << "[\r][\n]"
[Worker-2] org.apache.http.impl.conn.DefaultClientConnection 254 - Receiving response: HTTP/1.1 200 OK
[Worker-2] org.apache.http.impl.conn.DefaultClientConnection 257 - << HTTP/1.1 200 OK
[Worker-2] org.apache.http.impl.conn.DefaultClientConnection 260 - << Content-Length: 0
[Worker-2] org.apache.http.impl.client.DefaultRequestDirector 540 - Connection can be kept alive indefinitely
**[Worker-2] org.apache.http.impl.conn.DefaultClientConnection 154 - Connection 0.0.0.0:51211<->192.168.88.172:15010 shut down**
[Worker-2] org.apache.http.impl.conn.BasicClientConnectionManager 189 - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@12f65ce5DefaultClientConnection.shutdown (Connection 0.0.0.0:51210<->192.168.88.172:15010关闭)是否关闭了客户端的连接?它是如何被调用的?在代码中,从服务器收到响应(200OK)后,httpPost.releaseConnection()只在客户端代码中执行。
我应该怎么做才能保持连接处于已建立状态并重用它们,而不是为每个请求创建连接,然后它们进入TIME_WAIT。
任何帮助都将受到高度的感谢。谢谢。
发布于 2020-08-13 21:28:49
注意:调用EntityUtils.console(entity)会做更多的事情,应该使用:
/**
* Ensures that the entity content is fully consumed and
* the content stream, if exists, is closed.
*
* @param entity the entity to consume.
* @throws IOException if an error occurs reading the input stream
*
* @since 4.1
*/
public static void consume(final HttpEntity entity) throws IOException {
if (entity == null) {
return;
}
if (entity.isStreaming()) {
final InputStream inStream = entity.getContent();
if (inStream != null) {
inStream.close();
}
}
}发布于 2013-05-07 15:09:10
几天前解决了这个问题。
错误:在HttpClient对象上调用releaseConnection()。但是,避免它也不能解决问题。
更正:为保持连接活动(处于ESTABLISHED状态)而采取的操作是通过以下示例代码:
HttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
...
org.apache.http.HttpResponse response = client.executed(HttpPost) ;
org.apache.http.HttpEntity en = response.getEntity();
if (en != null) {
en.getContent().close();
}一旦接收到HttpResponse,为了重用和维护所创建的连接,只需要关闭http-response上的InputStream (en.getContent())。请勿对HttpPost或客户端对象调用任何其他release方法。
用JMeter load测试了这个处理,发现它工作正常。还没有观察到任何副作用!
https://stackoverflow.com/questions/16141387
复制相似问题