hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "qustmrhuang@163.com" <qustmrhu...@163.com>
Subject Fw:Fw:PoolingHttpClientConnectionManager how to reuse the connection
Date Thu, 23 Apr 2015 01:41:04 GMT


Hi,i use PoolingHttpClientConnectionManager with httpclient4.4 .
I see the code in MainClientExec.java,but i can't understand .




The respone entity is always not null and is stream,so the releaseConnection method cant be
invoked always.


 final HttpEntity entity = response.getEntity(); 
if (entity == null || !entity.isStreaming()) { 
// connection not needed and (assumed to be) in re-usable state 
connHolder.releaseConnection(); 
return new HttpResponseProxy(response, null); 
} else { 
return new HttpResponseProxy(response, connHolder); 
}


I have see the link
  http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/advanced.html#stateful_conn

In my program ,I did  not visit https website.




My code 
  for (int i=0;i<2;i++){ 
            new Thread(new Runnable() { 
                      public void run() { 
                               SendFile.test("http://tool.chinaz.com/", "start"); 
                               try { 
                                        .sleep(1000); 
                                        } catch (InterruptedException e) { 
                                            // TODO Auto-generated catch block 
                                        e.printStackTrace(); 
                                        } 
                                } 
                                    }).start(); 
}
public static void test(String url, String functionname) { 

HttpPost post = new HttpPost(url); 
CloseableHttpClient client = httpClient; 
CloseableHttpResponse r = null; 
try { 
r = client.execute(post, HttpClientContext.create()); 
HttpEntity entity = r.getEntity(); 
entity.getContent(); 
} catch (ClientProtocolException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
} 
}


private static PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

private static CloseableHttpClient httpClient; 
static { 
cm.setMaxTotal(200); 
cm.setDefaultMaxPerRoute(2); 
RequestConfig defaultRequestConfig = RequestConfig.custom() 
.setSocketTimeout(10000).setConnectTimeout(10000) 
.setConnectionRequestTimeout(50000).build(); 

httpClient = HttpClients.custom() 
.setDefaultRequestConfig(defaultRequestConfig) 
.setConnectionManager(cm).build(); 
} 




The log output is here .
- http-outgoing-1 << HTTP/1.1 200 OK 
- http-outgoing-1 << Cache-Control: public 
- http-outgoing-1 << Content-Type: text/html; charset=utf-8 
- http-outgoing-1 << Content-Encoding: gzip 
- http-outgoing-1 << Expires: Fri, 17 Apr 2015 03:50:27 GMT 
- http-outgoing-1 << Server: Microsoft-IIS/7.5 
- http-outgoing-1 << X-AspNet-Version: 4.0.30319 
- http-outgoing-1 << Date: Fri, 17 Apr 2015 03:20:27 GMT 
- http-outgoing-1 << Content-Length: 8507 
- Connection can be kept alive indefinitely




Any help in this issue would be much appreciated. 
Thanks
mrhuang





Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message