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 Javier Ramos <jr...@gmail.com> on 2010/11/13 11:30:30 UTC

HttpCore/HttpClient services integration in a J2EE Servlet container

Hello,

I am currently using HttpClient 3.1 inside a servlet that needs to retrieve
XML content from several webs, which must be a very common need.

I am thinking of moving my code  to HttpCore or HttpClient 4.

Before I make the transition I am researching for best practices using
HttpCore or HttpClient inside a servlet container.

I came up with the idea that it would be good to have a managed http client
service inside the J2EE container, in such a way that the applications could
get a client via JNDI lookup, in a similar way as they get connections from
a database connection pool. Ideally, from the J2EE console or from another
webapp, it would be nice to be able to monitor and administrate HTTP
connections to the different hosts: setup connection parameters for
different hosts at runtime, block slow performing hosts...

Does anybody know if there is any J2EE container offering such capabilities
?
Or some product / webapp that extends J2EE capabilities in such a way ?

Thanks,

Javier Ramos

Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Javier Ramos <jr...@gmail.com>.
Hi Oleg,

Yes, it is nice to be able to monitor the number of connections in use.
Would be even better to have it per host.

Thanks again,

Javier

2010/12/15 Oleg Kalnichevski <ol...@apache.org>

> On Wed, 2010-12-15 at 17:44 +0100, Javier Ramos wrote:
> > Thanks, Oleg
> >
> > I understand that reusing an HttpClient instance is a recommended
> practice,
> > ¿right?
> >
> > If we push this reuse strategy to the limit then it should be best to
> have
> > just one HttpClient with a ThreadSafeClientConnManager in a servlet only
> > J2EE application (no EJB's).
> >
> > I am thinking of wrapping this client in a singleton with
> >
> > 1.- A method to configure the conection manager, to be called at context
> > initialisation (max connections per host, max total connections, etc).
> > 2.- A getter for the HttpClient instance, to be used everywhere in the
> > application where an HttpClient is required
> > 3.- A method to cleanup everything, to be called on context destroy
> (would
> > call shutdown on the connection manager).
> >
> > Does this setup make sense ?
> >
>
> Makes sense to me.
>
> > If it does, having just one instance of HttpClient would provide the
> > opportunity to monitor it from a servlet. Being able to check the number
> of
> > connections established with each host would be a very nice feature, for
> > example. Or modifying the max connections per host at runtime. However,
> in
> > the javadocs I do not find any methods that can allow this information to
> be
> > obtained. Is it by design or am I missing something ?
> >
>
> What about ThreadSafeClientConnManager#getConnectionsInPool?
>
>
> http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html#getConnectionsInPool%28%29
>
> http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html#getConnectionsInPool%28%29
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2010-12-15 at 17:44 +0100, Javier Ramos wrote:
> Thanks, Oleg
> 
> I understand that reusing an HttpClient instance is a recommended practice,
> ¿right?
> 
> If we push this reuse strategy to the limit then it should be best to have
> just one HttpClient with a ThreadSafeClientConnManager in a servlet only
> J2EE application (no EJB's).
> 
> I am thinking of wrapping this client in a singleton with
> 
> 1.- A method to configure the conection manager, to be called at context
> initialisation (max connections per host, max total connections, etc).
> 2.- A getter for the HttpClient instance, to be used everywhere in the
> application where an HttpClient is required
> 3.- A method to cleanup everything, to be called on context destroy (would
> call shutdown on the connection manager).
> 
> Does this setup make sense ?
> 

Makes sense to me.

> If it does, having just one instance of HttpClient would provide the
> opportunity to monitor it from a servlet. Being able to check the number of
> connections established with each host would be a very nice feature, for
> example. Or modifying the max connections per host at runtime. However, in
> the javadocs I do not find any methods that can allow this information to be
> obtained. Is it by design or am I missing something ?
> 

What about ThreadSafeClientConnManager#getConnectionsInPool?

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html#getConnectionsInPool%28%29
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html#getConnectionsInPool%28%29

Oleg



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


Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Javier Ramos <jr...@gmail.com>.
Thanks, Oleg

I understand that reusing an HttpClient instance is a recommended practice,
¿right?

If we push this reuse strategy to the limit then it should be best to have
just one HttpClient with a ThreadSafeClientConnManager in a servlet only
J2EE application (no EJB's).

I am thinking of wrapping this client in a singleton with

1.- A method to configure the conection manager, to be called at context
initialisation (max connections per host, max total connections, etc).
2.- A getter for the HttpClient instance, to be used everywhere in the
application where an HttpClient is required
3.- A method to cleanup everything, to be called on context destroy (would
call shutdown on the connection manager).

Does this setup make sense ?

If it does, having just one instance of HttpClient would provide the
opportunity to monitor it from a servlet. Being able to check the number of
connections established with each host would be a very nice feature, for
example. Or modifying the max connections per host at runtime. However, in
the javadocs I do not find any methods that can allow this information to be
obtained. Is it by design or am I missing something ?


Thanks a lot,

Javier

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

> On Mon, 2010-11-15 at 13:21 +0100, Javier Ramos wrote:
> > Thanks for your feedback, Oleg
> >
> > We are doing with just a servlet container (Tomcat)
>
> My apologies, Javier. I mis-interpreted the subject of your message and
> assumed you were using a full-blown J2EE application server.
>
>
> > I guess that to follow your recommendation with minimal changes to our
> > productive environment we would have to try OpenEJB.
> > Using ThreadSafeClientConnManager inside a servlet container is also
> > discouraged, right ?
> >
>
> There is absolutely no need to drag in OpenEJB with all its dependencies
> just to maintain a pool of HTTP connections. I do not think the Servlet
> specification imposes any restrictions on thread synchronization in the
> web tier components.
>
> > Is anybody using such configurations, or is willing to drop a comment ? (
> > Tomcat + OpenEJB + HttpClient, or Tomcat + ThreadSafeClientConnManager )
> >
>
> I have used HttpClient 3 and HttpClient 4 with a pooling connection
> manager inside various servlet containers such as Tomcat and WebLogic
> without any issues.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2010-11-15 at 13:21 +0100, Javier Ramos wrote:
> Thanks for your feedback, Oleg
> 
> We are doing with just a servlet container (Tomcat)

My apologies, Javier. I mis-interpreted the subject of your message and
assumed you were using a full-blown J2EE application server.


> I guess that to follow your recommendation with minimal changes to our
> productive environment we would have to try OpenEJB.
> Using ThreadSafeClientConnManager inside a servlet container is also
> discouraged, right ?
> 

There is absolutely no need to drag in OpenEJB with all its dependencies
just to maintain a pool of HTTP connections. I do not think the Servlet
specification imposes any restrictions on thread synchronization in the
web tier components.  

> Is anybody using such configurations, or is willing to drop a comment ? (
> Tomcat + OpenEJB + HttpClient, or Tomcat + ThreadSafeClientConnManager )
> 

I have used HttpClient 3 and HttpClient 4 with a pooling connection
manager inside various servlet containers such as Tomcat and WebLogic
without any issues.

Oleg



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


Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Javier Ramos <jr...@gmail.com>.
Thanks for your feedback, Oleg

We are doing with just a servlet container (Tomcat)
I guess that to follow your recommendation with minimal changes to our
productive environment we would have to try OpenEJB.
Using ThreadSafeClientConnManager inside a servlet container is also
discouraged, right ?

Is anybody using such configurations, or is willing to drop a comment ? (
Tomcat + OpenEJB + HttpClient, or Tomcat + ThreadSafeClientConnManager )

Thanks again,

Javier

On Sun, Nov 14, 2010 at 12:53 PM, Oleg Kalnichevski <ol...@apache.org>wrote:

> On Sat, 2010-11-13 at 11:30 +0100, Javier Ramos wrote:
> > Hello,
> >
> > I am currently using HttpClient 3.1 inside a servlet that needs to
> retrieve
> > XML content from several webs, which must be a very common need.
> >
> > I am thinking of moving my code  to HttpCore or HttpClient 4.
> >
> > Before I make the transition I am researching for best practices using
> > HttpCore or HttpClient inside a servlet container.
> >
>
> My personal recommendation would be to wrap HttpClient or a custom HTTP
> client based on HttpCore into a stateless session bean. In both cases
> you should be using only a single connection per instance and let the
> container manage a pool of instances. You should not be using a pooling
> HTTP connection manager such ThreadSafeClientConnManager inside an EJB
> container.
>
>
> > I came up with the idea that it would be good to have a managed http
> client
> > service inside the J2EE container, in such a way that the applications
> could
> > get a client via JNDI lookup, in a similar way as they get connections
> from
> > a database connection pool. Ideally, from the J2EE console or from
> another
> > webapp, it would be nice to be able to monitor and administrate HTTP
> > connections to the different hosts: setup connection parameters for
> > different hosts at runtime, block slow performing hosts...
> >
> > Does anybody know if there is any J2EE container offering such
> capabilities
> > ?
> > Or some product / webapp that extends J2EE capabilities in such a way ?
> >
>
> None I am aware of
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: HttpCore/HttpClient services integration in a J2EE Servlet container

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Sat, 2010-11-13 at 11:30 +0100, Javier Ramos wrote:
> Hello,
> 
> I am currently using HttpClient 3.1 inside a servlet that needs to retrieve
> XML content from several webs, which must be a very common need.
> 
> I am thinking of moving my code  to HttpCore or HttpClient 4.
> 
> Before I make the transition I am researching for best practices using
> HttpCore or HttpClient inside a servlet container.
> 

My personal recommendation would be to wrap HttpClient or a custom HTTP
client based on HttpCore into a stateless session bean. In both cases
you should be using only a single connection per instance and let the
container manage a pool of instances. You should not be using a pooling
HTTP connection manager such ThreadSafeClientConnManager inside an EJB
container.


> I came up with the idea that it would be good to have a managed http client
> service inside the J2EE container, in such a way that the applications could
> get a client via JNDI lookup, in a similar way as they get connections from
> a database connection pool. Ideally, from the J2EE console or from another
> webapp, it would be nice to be able to monitor and administrate HTTP
> connections to the different hosts: setup connection parameters for
> different hosts at runtime, block slow performing hosts...
> 
> Does anybody know if there is any J2EE container offering such capabilities
> ?
> Or some product / webapp that extends J2EE capabilities in such a way ?
> 

None I am aware of

Oleg


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