You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Karl Wright <da...@gmail.com> on 2014/04/04 15:03:57 UTC

Under what conditions would you prefer org.apache.http.impl.client.HttpClientBuilder#setMaxConnTotal to org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setMaxTotal?

Hi Oleg,

We use a certain pattern a lot, which is the following:

- we have a pool of objects, which MCF manages, which needs HttpClient
instances to communicate with external systems
- we want the HttpClient instances to be reused, where possible, but it
only makes sense to reuse HttpClient instances within the context of one of
MCF's objects
- Each MCF object only directly uses one HttpClient object at a time

In the past, we did this by creating an HttpConnectionPool per MCF object,
and limiting it to size 1.  We would create HttpClient objects using the
"new HttpClient(HttpConnectionPool)" constructor, or the equivalent.

In the 4.3 world, our committers have tried to model this in two different
ways:

(a) Creating a PoolingHttpClientConnectionManager object, and calling
setMaxTotal(1) on it, and setting the connection manager when building the
HttpClient object, or
(b) Creating a PoolingHttpClientConnectionManager object, setting the
connection manager when building the HttpClient object, and using
setMaxConnTotal(1) in the builder

Which does the right thing?  Would either of these work equally well, or do
none of them work in the way intended?

Thanks in advance,
Karl

Re: Under what conditions would you prefer org.apache.http.impl.client.HttpClientBuilder#setMaxConnTotal to org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setMaxTotal?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2014-04-04 at 09:03 -0400, Karl Wright wrote:
> Hi Oleg,
> 
> We use a certain pattern a lot, which is the following:
> 
> - we have a pool of objects, which MCF manages, which needs HttpClient
> instances to communicate with external systems
> - we want the HttpClient instances to be reused, where possible, but it
> only makes sense to reuse HttpClient instances within the context of one of
> MCF's objects
> - Each MCF object only directly uses one HttpClient object at a time
> 
> In the past, we did this by creating an HttpConnectionPool per MCF object,
> and limiting it to size 1.  We would create HttpClient objects using the
> "new HttpClient(HttpConnectionPool)" constructor, or the equivalent.
> 
> In the 4.3 world, our committers have tried to model this in two different
> ways:
> 
> (a) Creating a PoolingHttpClientConnectionManager object, and calling
> setMaxTotal(1) on it, and setting the connection manager when building the
> HttpClient object, or
> (b) Creating a PoolingHttpClientConnectionManager object, setting the
> connection manager when building the HttpClient object, and using
> setMaxConnTotal(1) in the builder
> 
> Which does the right thing?  Would either of these work equally well, or do
> none of them work in the way intended?
> 

Neither are wrong. Both ways are largely the same. HttpClientBuilder
creates an instance of PoolingHttpClientConnectionManager internally if
no connection manager has been explicitly set. However if you want to be
able to tweak connection manager settings at runtime the former approach
should be used as you will need a reference to the connection manager to
do so.  

If what you want to a single persistent connection have you considered
using BasicHttpClientConnectionManager instead of
PoolingHttpClientConnectionManager?

Oleg



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