David
A code snippet is worth a thousand words. How about this?
static class WorkerThread extends Thread {
// Reference to a common/global HttpClient instance
private final HttpClient httpclient;
// Thread private HTTP state
private final HttpState httpstate;
// Thread private host config (optional)
private final HostConfiguration hostconfig;
private Throwable exception = null;
public WorkerThread(
final HttpClient httpclient, final String hostname) {
super();
this.httpclient = httpclient;
this.httpstate = new HttpState();
this.hostconfig = new HostConfiguration();
this.hostconfig.setHost(hostname);
}
public void run() {
// Note the use of relative URL
GetMethod httpget = new GetMethod("/stuff");
try {
this.httpclient.executeMethod(
this.hostconfig, httpget, this.httpstate);
// Do something useful with the response body
} catch (IOException ex) {
// keep the exception for postmortem analysis
this.exception = ex;
} finally {
httpget.releaseConnection();
}
}
}
public static void main(String[] args) throws Exception {
MultiThreadedHttpConnectionManager connman =
new MultiThreadedHttpConnectionManager();
HttpClient httpclient = new HttpClient(connman);
WorkerThread[] workers = new WorkerThread[] {
new WorkerThread(httpclient, "host1"),
new WorkerThread(httpclient, "host2"),
new WorkerThread(httpclient, "host3")
};
for (int i = 0; i < workers.length; i++) {
workers[i].start();
}
for (int i = 0; i < workers.length; i++) {
workers[i].join();
}
}
As you can see you the worker threads encapsulate all the private data:
cookies, login credentials, host configuration, and host specific
parameters.
Oleg
On Thu, 2005-01-27 at 12:26 -0500, dhay@lexmark.com wrote:
> Hi Oleg,
>
> Yep, that helps in terms of submitting the right cookie...but then I have a
> problem GETTING the session back for that particular thread!
>
> I want everything to be happening at once, so obviously don't want to
> synchronize on the whole call. But as the cookie seems to be returned in
> the HttpClient, when I have lots of threads running, I can't guarantee that
> when I look in the client after I read the response that it holds the
> session cookie for that particular thread - right? (I'm calling
> _httpClient.getState().getCookies() to get the session cookie).
>
> Or am I missing something? Is there any way to get the session for that
> particular method?
>
> cheers,
>
> David
>
>
>
>
> |---------+---------------------------->
> | | Oleg Kalnichevski|
> | | <olegk@apache.org|
> | | > |
> | | |
> | | 01/26/2005 06:06 |
> | | PM |
> | | Please respond to|
> | | "HttpClient User |
> | | Discussion" |
> | | |
> |---------+---------------------------->
> >-----------------------------------------------------------------------------------------------------------------------|
> |
|
> | To: HttpClient User Discussion <httpclient-user@jakarta.apache.org>
|
> | cc:
|
> | Subject: Re: Cookies with multithreaded connection manager - [was Re: Any
equivalent to setReuseAddress() to |
> | true - ie reuse sockets in TIME_WAIT state]
|
> >-----------------------------------------------------------------------------------------------------------------------|
>
>
>
>
> David,
>
> Create a instance of HttpState per thread and pass it to
> HttpClient#executeMethod as a parameter. This should do the trick
>
> Oleg
>
> On Wed, 2005-01-26 at 17:54 -0500, dhay@lexmark.com wrote:
> > Hi,
> >
> > Thanks for the tip. I have tried this, but ran into a problem with
> cookies
> > when using it.
> >
> > I created a single HttpClient instance, with a
> > MultiThreadedHttpConnectionManager. I pass this client to each thread
> that
> > I start.
> >
> > However, I also need to keep track of session information for each
> thread,
> > and am not sure how to do that in this model (I get another connection
> from
> > my caller, and hence another thread spawned, which must be part of the
> same
> > session). I was just calling setState as follows if the thread was part
> of
> > a particular session like this:
> >
> > if (_session != null)
> > {
> > // adding session cookie previously saved");
> > HttpState initialState = new HttpState();
> > initialState.addCookie(_session);
> > _httpClient.setState(initialState);
> > }
> >
> > This obviously doesn't work with multiple threads accessing the same
> > HttpClient.
> >
> > What am I missing? Is there another way to do cookies?
> >
> > thanks,
> >
> > David
> >
> >
> >
> > |---------+---------------------------->
> > | | Oleg Kalnichevski|
> > | | <olegk@apache.org|
> > | | > |
> > | | |
> > | | 01/24/2005 06:13 |
> > | | PM |
> > | | Please respond to|
> > | | "HttpClient User |
> > | | Discussion" |
> > | | |
> > |---------+---------------------------->
> >
> >-----------------------------------------------------------------------------------------------------------------------|
>
> > |
> |
> > | To: HttpClient User Discussion
> <httpclient-user@jakarta.apache.org>
> |
> > | cc:
> |
> > | Subject: Re: Any equivalent to setReuseAddress() to true - ie
> reuse sockets in TIME_WAIT state |
> >
> >-----------------------------------------------------------------------------------------------------------------------|
>
> >
> >
> >
> >
> > David,
> >
> > What kind of connection manager are you using? If yes, consider using
> > the so called multithreaded connection manager that maintains a pool of
> > connections:
> >
> > http://jakarta.apache.org/commons/httpclient/3.0/threading.html
> >
> > Hope this helps,
> >
> > Oleg
> >
> > On Mon, 2005-01-24 at 15:04 -0500, dhay@lexmark.com wrote:
> > > Hi,
> > >
> > > I am creating alot of connections to Tomcat 5 using HttpClient. As a
> > > result, I have a lot of sockets in the TIME_WAIT state, which lead to a
> > > BindException - Connection In use.
> > >
> > > Is there any way to get the HttpClient to reuse these sockets akin to
> > using
> > > setReuseAddress(true) when using a plain socket?
> > >
> > > Any other suggestions?
> > >
> > > cheers,
> > >
> > > David
> > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > 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
> >
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > 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
>
>
>
>
>
>
> ---------------------------------------------------------------------
> 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
|