You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Zhan Zhifeng <zh...@gmail.com> on 2012/11/14 12:38:03 UTC

how to release leased connection in httpclient PoolingClientConnectionManager

Hi,

I run a crawler with httpclient. PoolingClientConnectionManager used in the
process of crawling.
I monitor the PoolStats with one monitor thread. And when my crawling
website list is zero,
and the ps_.getLeased() is always not be zero.
But i am not sure that the crawling job is finished or not. PS:
ps_.getAvailable() is zero.

refer the
https://issues.apache.org/jira/browse/HTTPCLIENT-1104?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
New connection pool and pooling connection manager implementations now
expose pool stats such number of leased & available connections.
Stats can be obtained either for a particular route or the entire pool.

I also do the following method to try to release the connection in pool,
but it seems not work:
(1) _httpclient.getParams().
setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 100000);
(2) In monitor thread, connMgr.closeExpiredConnections();
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);

I am wandering if there is any active crawling thread using these leased
connections,
and how can i know it or how can i operate these leased connections?
And then i can finish my crawling job more accurately.

Thanks a lot.

Re: how to release leased connection in httpclient PoolingClientConnectionManager

Posted by Zhan Zhifeng <zh...@gmail.com>.
hi, Oleg

How can I "_always_ response content "?

I have do these below:
HttpEntity entity = response.getEntity();
if (entity != null && header_[0].getValue().contains("text/html")) {
String html_ = EntityUtils.toString(entity, "UTF-8");
                EntityUtils.consume(entity);
} else {

}




2012/11/15 Oleg Kalnichevski <ol...@apache.org>

> On Wed, 2012-11-14 at 19:38 +0800, Zhan Zhifeng wrote:
> > Hi,
> >
> > I run a crawler with httpclient. PoolingClientConnectionManager used in
> the
> > process of crawling.
> > I monitor the PoolStats with one monitor thread. And when my crawling
> > website list is zero,
> > and the ps_.getLeased() is always not be zero.
> > But i am not sure that the crawling job is finished or not. PS:
> > ps_.getAvailable() is zero.
> >
> > refer the
> >
> https://issues.apache.org/jira/browse/HTTPCLIENT-1104?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
> > New connection pool and pooling connection manager implementations now
> > expose pool stats such number of leased & available connections.
> > Stats can be obtained either for a particular route or the entire pool.
> >
> > I also do the following method to try to release the connection in pool,
> > but it seems not work:
> > (1) _httpclient.getParams().
> > setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 100000);
> > (2) In monitor thread, connMgr.closeExpiredConnections();
> > connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
> >
> > I am wandering if there is any active crawling thread using these leased
> > connections,
> > and how can i know it or how can i operate these leased connections?
> > And then i can finish my crawling job more accurately.
> >
> > Thanks a lot.
>
> I think your code may be leaking connections. Just make sure that you
> _always_ response content (even if you do not need it) and it will
> automatically take care of connection release.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>


-- 
网络与媒体实验室·浙江大学
占志峰
13588163850

Re: how to release leased connection in httpclient PoolingClientConnectionManager

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2012-11-16 at 00:05 +0800, Zhan Zhifeng wrote:
> hi, Oleg
> 
> How can I "_always_ response content "?
> 
> I have do these below:
> HttpEntity entity = response.getEntity();
> if (entity != null && header_[0].getValue().
> contains("text/html")) {
>     String html_ = EntityUtils.toString(entity, "UTF-8");
>     EntityUtils.consume(entity);
> } else {
>     EntityUtils.consume(entity);
> }
> 
> 

Option 1
----------------------------
HttpGet httpGet = new HttpGet("http://targethost/stuff");
ResponseHandler<T> responseHandler = new MyResponseHandler();
// Connection will be released automatically 
T result = httpclient.execute(httpGet, responseHandler);
----------------------------

Option 2
---------------------------- 
HttpGet httpGet = new HttpGet("http://targethost/stuff");
HttpResponse response = httpclient.execute(httpGet);
try {
    HttpEntity entity = response1.getEntity();
    // do something useful with the response body
    // and manually ensure it is fully consumed
} finally {
    EntityUtils.consume(response.getEntity());
}
---------------------------- 

Option 3 (HttpClient 3.x style)
---------------------------- 
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
HttpResponse response = httpclient.execute(httpGet);
try {
    System.out.println(response1.getStatusLine());
    HttpEntity entity = response1.getEntity();
    // do something useful with the response body
} finally {
    httpGet.releaseConnection();
}
---------------------------- 

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to release leased connection in httpclient PoolingClientConnectionManager

Posted by Zhan Zhifeng <zh...@gmail.com>.
hi, Oleg

How can I "_always_ response content "?

I have do these below:
HttpEntity entity = response.getEntity();
if (entity != null && header_[0].getValue().
contains("text/html")) {
    String html_ = EntityUtils.toString(entity, "UTF-8");
    EntityUtils.consume(entity);
} else {
    EntityUtils.consume(entity);
}


2012/11/15 Oleg Kalnichevski <ol...@apache.org>

> On Wed, 2012-11-14 at 19:38 +0800, Zhan Zhifeng wrote:
> > Hi,
> >
> > I run a crawler with httpclient. PoolingClientConnectionManager used in
> the
> > process of crawling.
> > I monitor the PoolStats with one monitor thread. And when my crawling
> > website list is zero,
> > and the ps_.getLeased() is always not be zero.
> > But i am not sure that the crawling job is finished or not. PS:
> > ps_.getAvailable() is zero.
> >
> > refer the
> >
> https://issues.apache.org/jira/browse/HTTPCLIENT-1104?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
> > New connection pool and pooling connection manager implementations now
> > expose pool stats such number of leased & available connections.
> > Stats can be obtained either for a particular route or the entire pool.
> >
> > I also do the following method to try to release the connection in pool,
> > but it seems not work:
> > (1) _httpclient.getParams().
> > setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 100000);
> > (2) In monitor thread, connMgr.closeExpiredConnections();
> > connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
> >
> > I am wandering if there is any active crawling thread using these leased
> > connections,
> > and how can i know it or how can i operate these leased connections?
> > And then i can finish my crawling job more accurately.
> >
> > Thanks a lot.
>
> I think your code may be leaking connections. Just make sure that you
> _always_ response content (even if you do not need it) and it will
> automatically take care of connection release.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>


-- 
网络与媒体实验室·浙江大学
占志峰
13588163850

Re: how to release leased connection in httpclient PoolingClientConnectionManager

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2012-11-14 at 19:38 +0800, Zhan Zhifeng wrote:
> Hi,
> 
> I run a crawler with httpclient. PoolingClientConnectionManager used in the
> process of crawling.
> I monitor the PoolStats with one monitor thread. And when my crawling
> website list is zero,
> and the ps_.getLeased() is always not be zero.
> But i am not sure that the crawling job is finished or not. PS:
> ps_.getAvailable() is zero.
> 
> refer the
> https://issues.apache.org/jira/browse/HTTPCLIENT-1104?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
> New connection pool and pooling connection manager implementations now
> expose pool stats such number of leased & available connections.
> Stats can be obtained either for a particular route or the entire pool.
> 
> I also do the following method to try to release the connection in pool,
> but it seems not work:
> (1) _httpclient.getParams().
> setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 100000);
> (2) In monitor thread, connMgr.closeExpiredConnections();
> connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
> 
> I am wandering if there is any active crawling thread using these leased
> connections,
> and how can i know it or how can i operate these leased connections?
> And then i can finish my crawling job more accurately.
> 
> Thanks a lot.

I think your code may be leaking connections. Just make sure that you
_always_ response content (even if you do not need it) and it will
automatically take care of connection release.

Oleg 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org