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 Re:Re: Fw:Fw:PoolingHttpClientConnectionManager how to reuse the connection
Date Fri, 24 Apr 2015 02:53:54 GMT
I see the code about  response close. AbortConnection not releaseConnection ,so the connection
can be re-used.I am confused how the connection be re-used.
public void close() throws IOException {
        if (this.connHolder != null) {
            this.connHolder.abortConnection();
        }
    }








At 2015-04-24 03:23:19, "Oleg Kalnichevski" <olegk@apache.org> wrote:
>On Thu, 2015-04-23 at 09:41 +0800, qustmrhuang@163.com wrote:
>> 
>> 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
>> 
>
>I am not quite sure I understand the issue. You should not even be using
>#releaseConnection in the first place. It is provided for compatibility
>with HC 3.x. You should always close CloseableHttpResponse in a
>try-finally or try-with-resources.
>
>Oleg
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message