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 Puneet Lakhina <pu...@gmail.com> on 2007/10/17 11:36:35 UTC
Should Multiple threads use same HttpClient object
Hi,
My current code using httpclient looks something like this:
public class MyClass {
public void myMethod(String name,String value) {
/*This Method is called repeatedly*/
Thread t = new MyPostThread(name,value);
t.start();
}
public static MyPostThread extends Thread {
String name;
String value;
public MyPostThread(String name,String value) {
this.name=name;
this.value=value;
}
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();
}
}
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?
Any brickkbats, suggestions,comments would be helpful.
--
Puneet
http://sahyog.blogspot.com/
Re: Should Multiple threads use same HttpClient object
Posted by Roland Weber <os...@dubioso.net>.
Puneet Lakhina wrote:
> I hope I am interpreting it right.
yes
> 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
>>
>>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
Re: Should Multiple threads use same HttpClient object
Posted by Puneet Lakhina <pu...@gmail.com>.
On 10/18/07, Roland Weber <os...@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/
Re: Should Multiple threads use same HttpClient object
Posted by Roland Weber <os...@dubioso.net>.
Puneet Lakhina wrote:
> On 10/18/07, Roland Weber <os...@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.
>
>
> Isnt postMethod.releaseConnection() done to prevent that??
Sorry, my wording was misleading. I should have written
"until they are closed by garbage collection". To release is
not to close, as you will learn from the "Connection Persistence"
section of the Performance Tuning Guide:
http://jakarta.apache.org/httpcomponents/httpclient-3.x/performance.html
cheers,
Roland
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
Re: Should Multiple threads use same HttpClient object
Posted by Puneet Lakhina <pu...@gmail.com>.
On 10/18/07, Roland Weber <os...@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.
Isnt postMethod.releaseConnection() done to prevent that??
--
Puneet
http://sahyog.blogspot.com/
Re: Should Multiple threads use same HttpClient object
Posted by Roland Weber <os...@dubioso.net>.
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
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