hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Puneet Lakhina" <puneet.lakh...@gmail.com>
Subject Re: Should Multiple threads use same HttpClient object
Date Fri, 19 Oct 2007 06:02:18 GMT
On 10/18/07, Roland Weber <ossfwot@dubioso.net> wrote:
>
> Puneet Lakhina wrote:
> >  public void run() {
> >
> >         HttpClient client = new HttpClient();
> >         PostMethod postMethod  = new PostMethod(sameURL);
> >         /*Set parameters in post method*/
> >         client.executeMethod();
> >          /*Do something with the response*/
> >         postMethod.releaseConnection();
> > }
>
> This will leak a connection for every request, until they
> are released by garbage collection.
>
> > I am a little confused as to how the connection management takes place.
> > Should i be using the same HttpClient object across different threads?
> > Should I be in that case using the MultiThreadedConnectionManager?
>
> Yes and yes. You are looking for the Threading Guide:
> http://jakarta.apache.org/httpcomponents/httpclient-3.x/threading.html


I hope I am interpreting it right. If i am making threads by implementing
Runnable, then the HttpClient instance should be a static variable,
something like the following.
public MyThreads implements Runnable{
private static HttpClient client = new HttpClient(new
MultiThreadedHttpConnectionManager());
public void run() {
try {
client.executeMethod(method);
}finally {
method.releaseConnection();
}
}
}

hope that helps,
>   Roland
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>


-- 
Puneet
http://sahyog.blogspot.com/

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