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 "Wittmann Armin (BI)" <ar...@id.ethz.ch> on 2006/11/21 14:22:59 UTC

usage of MultiThreadedHttpConnectionManager inside J2EE-Framework

Hi

I am writing a small J2EE-based Framework that allows  in a very
simple way to retrieve results from a http-request.

public interface HttpRequests {
   public String post(URL url);
   public String post(URL url, Properties nameValuePairs);

   public String get(URL url);
   public String get(URL url, Properties nameValuePairs);
} /* simplyfied */

The implementation of the interface will be at hand to the
developer inside an Enterprise Java Bean. Of course it should
be very performant and I decided to use commons-httpclient.
I tried to figure out how I can use the 
	MultiThreadedHttpConnectionManager
but at the same time not violating the J2EE paradigmas inside 
an EJB-implementation.

The example given in:
	
http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src
/examples/MultiThreadedExample.java?view=markup

is not very suitable, since the host is a (almost) fixed value. Or is
the
meaning that I will make a
	httpClient.getHostConfiguration().setHost(...)
or even
	httpClient.setHostConfiguration(...)
for every call to a function of the interface above or its 
implementation respectively? 
	
The other problem is that inside a stateless EJB it is not allowed to
define non-final but static fields since every instance of the EJB
(inside the
container) has access to the this field. This provokes race
conditions...
But because the implementation of 
	MultiThreadedHttpConnectionManager
seems to be thread safe one might make an exception here. 
This poses the question if this static field should hold an instance of
	MultiThreadedHttpConnectionManager
or possibly of
	HttpClient
?

I hope I will not start a large discussion thread. Unfortunately
I am missing detailed documentation for this kind of J2EE-application.

Thanks for helping

Armin 

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


RE: usage of MultiThreadedHttpConnectionManager insideJ2EE-Framework

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2006-11-21 at 14:49 +0100, Wittmann Armin (BI) wrote:
> Hi Oleg
> 
> thank you for your fast answer. 
> 
> > Do not use MultiThreadedHttpConnectionManager inside an EJB container.
> > Wrap an instance of HttpClient with a 
> > SimpleHttpConnectionManager into a
> > stateless session bean and let the EJB container do the connection
> > pooling for you. 
> This I have done so far :-)
> and it works fine.
> I am making for each reaquest a new instance of HttpClient and I am
> instantianing it every time with a new instance of
> SimpleHttpConnectionManager.
> 

Actually you may want to re-use HttpClient and the associated connection
manager, especially if your application talks to the same target host
and you want to make use of persistent connections. Make HttpClient an
instance variable of the stateless session bean. The EJB container will
take care of the access synchronization. All you have to do is to make
sure the HTTP state information is reset prior to each new HTTP
request.  

> > 
> > Make sure you reset the associated HttpState prior to each HTTP method
> > execution, though
> In the API I did not find out how to reset this state?
> Can you help me?
> 

This is the simples approach:

httpclient.setState(new HttpState());

Oleg

> Armin
> 
> ---------------------------------------------------------------------
> 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: usage of MultiThreadedHttpConnectionManager insideJ2EE-Framework

Posted by "Wittmann Armin (BI)" <ar...@id.ethz.ch>.
Hi Oleg

thank you for your fast answer. 

> Do not use MultiThreadedHttpConnectionManager inside an EJB container.
> Wrap an instance of HttpClient with a 
> SimpleHttpConnectionManager into a
> stateless session bean and let the EJB container do the connection
> pooling for you. 
This I have done so far :-)
and it works fine.
I am making for each reaquest a new instance of HttpClient and I am
instantianing it every time with a new instance of
SimpleHttpConnectionManager.

> 
> Make sure you reset the associated HttpState prior to each HTTP method
> execution, though
In the API I did not find out how to reset this state?
Can you help me?

Armin

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


Re: usage of MultiThreadedHttpConnectionManager inside J2EE-Framework

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2006-11-21 at 14:22 +0100, Wittmann Armin (BI) wrote:
> Hi
> 
> I am writing a small J2EE-based Framework that allows  in a very
> simple way to retrieve results from a http-request.
> 
> public interface HttpRequests {
>    public String post(URL url);
>    public String post(URL url, Properties nameValuePairs);
> 
>    public String get(URL url);
>    public String get(URL url, Properties nameValuePairs);
> } /* simplyfied */
> 
> The implementation of the interface will be at hand to the
> developer inside an Enterprise Java Bean. Of course it should
> be very performant and I decided to use commons-httpclient.
> I tried to figure out how I can use the 
> 	MultiThreadedHttpConnectionManager
> but at the same time not violating the J2EE paradigmas inside 
> an EJB-implementation.
> 
> The example given in:
> 	
> http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src
> /examples/MultiThreadedExample.java?view=markup
> 
> is not very suitable, since the host is a (almost) fixed value. Or is
> the
> meaning that I will make a
> 	httpClient.getHostConfiguration().setHost(...)
> or even
> 	httpClient.setHostConfiguration(...)
> for every call to a function of the interface above or its 
> implementation respectively? 
> 	
> The other problem is that inside a stateless EJB it is not allowed to
> define non-final but static fields since every instance of the EJB
> (inside the
> container) has access to the this field. This provokes race
> conditions...
> But because the implementation of 
> 	MultiThreadedHttpConnectionManager
> seems to be thread safe one might make an exception here. 
> This poses the question if this static field should hold an instance of
> 	MultiThreadedHttpConnectionManager
> or possibly of
> 	HttpClient
> ?
> 
> I hope I will not start a large discussion thread. Unfortunately
> I am missing detailed documentation for this kind of J2EE-application.
> 
> Thanks for helping
> 
> Armin 
> 

Armin,

Do not use MultiThreadedHttpConnectionManager inside an EJB container.
Wrap an instance of HttpClient with a SimpleHttpConnectionManager into a
stateless session bean and let the EJB container do the connection
pooling for you. 

Make sure you reset the associated HttpState prior to each HTTP method
execution, though

Hope this helps

Oleg



> ---------------------------------------------------------------------
> 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