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 Ankur Shah <an...@gmail.com> on 2008/03/21 06:05:53 UTC

Apache HttpClient - Using custom socket factory for failover support

I am using httpclient to connect to a remote server. Now for each primary
server there's also a backup failover server that i must talk to if
connection attempts with the primary server fails. To achieve
this what i'd ideally like to do is have HttpClient use a custom factory
that tries to create a socket connection with the primary server and if that
fails (or times out), it'll go about  talking to backup server (and retry a
few times before giving up altogether).

Now, searching on the web, i did find references to a
MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory) that is
presumably in apache contrib package. But what that does is that in the
overriden
createSocket method, it takes host as a param (among other thing) and then
simply does a InetAddress.getAllByName which is not what i want. From what i
understand this caters to a DNS based failover which is not the case here.

What i'd ideally like to do is create a factory and have a createSocket
method that takes primary and secondary host and port as args and then tries
both. I am not sure though how can i make HttpConnection class
invoke my method.

Re: Apache HttpClient - Using custom socket factory for failover support

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2008-03-25 at 14:44 -0700, Ankur Shah wrote:
> Thanks oleg,
> 
> Do you see any issues in the approach that i was thinking about (and have
> implemented already) other than the fact that its not very clean?

Ankur,

The problem is the connection manager can only catch connect timeout
exceptions, but you may also want to retry requests against the
secondary host in case of other I/O failures as well (for instance, when
the primary host is able to accept connections but is unable to process
requests)

>  Could you
> elaborate a bit more on what you mean by creating service on top of
> httpclient, (i understand what it'll do but just didn't know what/how should
> i register that with httpclient)?
> 

There's no need to register anything. Just implement whatever fail-over
logic is appropriate for you application on top of HttpClient

Oleg


> On Tue, Mar 25, 2008 at 1:10 PM, Oleg Kalnichevski <ol...@apache.org> wrote:
> 
> >
> > On Mon, 2008-03-24 at 15:46 -0700, Ankur Shah wrote:
> > > Hi Oleg,
> > >
> > > Thanks for the response. The server that i am running (acting as a
> > client in
> > > this particular case) is working with multiple servers (hosting
> > different
> > > webservices) with each having a primary and a failover pair. From this
> > > perspective the thought was that rather than having different classes
> > (all
> > > having their own business logic around invoking the service) handle the
> > > connection failed attempt, somehow have some central code that attempts
> > to
> > > connect to the primary and fails over to the backup. I also wanted to
> > make
> > > sure that in case of a connection failure with primary, a new pool is
> > > established against the backup server.
> > >
> > > From this perspective, an after thought that I had was this. I was going
> > to
> > > extend the MultiThreadedHttpConnectionManager and have a failover
> > capable
> > > connection manager. In there, i was thinking about exposing a method (or
> > a
> > > new constructor) that allows backup host and ports to be added. This
> > manager
> > > will cache a map of primary and backup server. In the overriden
> > > getConnectionWithTimeout method, i'd have super return me a connection
> > that
> > > i'll check and make sure that is valid and if not, i'll go to my cache,
> > get
> > > the backup host config and open up a connection to that.
> > >
> > > Let me know if you think this would be a good idea or not.
> > >
> > > Ankur
> > >
> >
> > Ankur
> >
> > My recommendation would be to implement the fail-over support as a
> > service on top of HttpClient
> >
> > (1) execute a request against the primary host
> > (2) if an exception is thrown, see whether it can be treated as
> > recoverable
> > (3) if so, mark the primary host as unavailable for a certain period
> > time and retry the request against the secondary host
> > (4) when the quarantine time elapses, mark the primary host as
> > potentially available
> > (5) repeat
> >
> > Hope this helps
> >
> > Oleg
> >
> >
> > > On Mon, Mar 24, 2008 at 12:47 PM, Oleg Kalnichevski <ol...@apache.org>
> > > wrote:
> > >
> > > >
> > > > On Thu, 2008-03-20 at 22:05 -0700, Ankur Shah wrote:
> > > > > I am using httpclient to connect to a remote server. Now for each
> > > > primary
> > > > > server there's also a backup failover server that i must talk to if
> > > > > connection attempts with the primary server fails. To achieve
> > > > > this what i'd ideally like to do is have HttpClient use a custom
> > factory
> > > > > that tries to create a socket connection with the primary server and
> > if
> > > > that
> > > > > fails (or times out), it'll go about  talking to backup server (and
> > > > retry a
> > > > > few times before giving up altogether).
> > > > >
> > > > > Now, searching on the web, i did find references to a
> > > > > MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory)
> > that
> > > > is
> > > > > presumably in apache contrib package. But what that does is that in
> > the
> > > > > overriden
> > > > > createSocket method, it takes host as a param (among other thing)
> > and
> > > > then
> > > > > simply does a InetAddress.getAllByName which is not what i want.
> > From
> > > > what i
> > > > > understand this caters to a DNS based failover which is not the case
> > > > here.
> > > > >
> > > > > What i'd ideally like to do is create a factory and have a
> > createSocket
> > > > > method that takes primary and secondary host and port as args and
> > then
> > > > tries
> > > > > both. I am not sure though how can i make HttpConnection class
> > > > > invoke my method.
> > > >
> > > > Ankur,
> > > >
> > > > What's the point of trying to implement the failover support on the
> > > > ProtocolSocketFactory level? What's wrong with just retrying failed
> > > > requests against a secondary server in case the primary one is
> > > > unavailable?
> > > >
> > > > Oleg
> > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > > >
> > > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >


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


Re: Apache HttpClient - Using custom socket factory for failover support

Posted by Ankur Shah <an...@gmail.com>.
Thanks oleg,

Do you see any issues in the approach that i was thinking about (and have
implemented already) other than the fact that its not very clean? Could you
elaborate a bit more on what you mean by creating service on top of
httpclient, (i understand what it'll do but just didn't know what/how should
i register that with httpclient)?

On Tue, Mar 25, 2008 at 1:10 PM, Oleg Kalnichevski <ol...@apache.org> wrote:

>
> On Mon, 2008-03-24 at 15:46 -0700, Ankur Shah wrote:
> > Hi Oleg,
> >
> > Thanks for the response. The server that i am running (acting as a
> client in
> > this particular case) is working with multiple servers (hosting
> different
> > webservices) with each having a primary and a failover pair. From this
> > perspective the thought was that rather than having different classes
> (all
> > having their own business logic around invoking the service) handle the
> > connection failed attempt, somehow have some central code that attempts
> to
> > connect to the primary and fails over to the backup. I also wanted to
> make
> > sure that in case of a connection failure with primary, a new pool is
> > established against the backup server.
> >
> > From this perspective, an after thought that I had was this. I was going
> to
> > extend the MultiThreadedHttpConnectionManager and have a failover
> capable
> > connection manager. In there, i was thinking about exposing a method (or
> a
> > new constructor) that allows backup host and ports to be added. This
> manager
> > will cache a map of primary and backup server. In the overriden
> > getConnectionWithTimeout method, i'd have super return me a connection
> that
> > i'll check and make sure that is valid and if not, i'll go to my cache,
> get
> > the backup host config and open up a connection to that.
> >
> > Let me know if you think this would be a good idea or not.
> >
> > Ankur
> >
>
> Ankur
>
> My recommendation would be to implement the fail-over support as a
> service on top of HttpClient
>
> (1) execute a request against the primary host
> (2) if an exception is thrown, see whether it can be treated as
> recoverable
> (3) if so, mark the primary host as unavailable for a certain period
> time and retry the request against the secondary host
> (4) when the quarantine time elapses, mark the primary host as
> potentially available
> (5) repeat
>
> Hope this helps
>
> Oleg
>
>
> > On Mon, Mar 24, 2008 at 12:47 PM, Oleg Kalnichevski <ol...@apache.org>
> > wrote:
> >
> > >
> > > On Thu, 2008-03-20 at 22:05 -0700, Ankur Shah wrote:
> > > > I am using httpclient to connect to a remote server. Now for each
> > > primary
> > > > server there's also a backup failover server that i must talk to if
> > > > connection attempts with the primary server fails. To achieve
> > > > this what i'd ideally like to do is have HttpClient use a custom
> factory
> > > > that tries to create a socket connection with the primary server and
> if
> > > that
> > > > fails (or times out), it'll go about  talking to backup server (and
> > > retry a
> > > > few times before giving up altogether).
> > > >
> > > > Now, searching on the web, i did find references to a
> > > > MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory)
> that
> > > is
> > > > presumably in apache contrib package. But what that does is that in
> the
> > > > overriden
> > > > createSocket method, it takes host as a param (among other thing)
> and
> > > then
> > > > simply does a InetAddress.getAllByName which is not what i want.
> From
> > > what i
> > > > understand this caters to a DNS based failover which is not the case
> > > here.
> > > >
> > > > What i'd ideally like to do is create a factory and have a
> createSocket
> > > > method that takes primary and secondary host and port as args and
> then
> > > tries
> > > > both. I am not sure though how can i make HttpConnection class
> > > > invoke my method.
> > >
> > > Ankur,
> > >
> > > What's the point of trying to implement the failover support on the
> > > ProtocolSocketFactory level? What's wrong with just retrying failed
> > > requests against a secondary server in case the primary one is
> > > unavailable?
> > >
> > > Oleg
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache HttpClient - Using custom socket factory for failover support

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2008-03-24 at 15:46 -0700, Ankur Shah wrote:
> Hi Oleg,
> 
> Thanks for the response. The server that i am running (acting as a client in
> this particular case) is working with multiple servers (hosting different
> webservices) with each having a primary and a failover pair. From this
> perspective the thought was that rather than having different classes (all
> having their own business logic around invoking the service) handle the
> connection failed attempt, somehow have some central code that attempts to
> connect to the primary and fails over to the backup. I also wanted to make
> sure that in case of a connection failure with primary, a new pool is
> established against the backup server.
> 
> From this perspective, an after thought that I had was this. I was going to
> extend the MultiThreadedHttpConnectionManager and have a failover capable
> connection manager. In there, i was thinking about exposing a method (or a
> new constructor) that allows backup host and ports to be added. This manager
> will cache a map of primary and backup server. In the overriden
> getConnectionWithTimeout method, i'd have super return me a connection that
> i'll check and make sure that is valid and if not, i'll go to my cache, get
> the backup host config and open up a connection to that.
> 
> Let me know if you think this would be a good idea or not.
> 
> Ankur
> 

Ankur

My recommendation would be to implement the fail-over support as a
service on top of HttpClient

(1) execute a request against the primary host
(2) if an exception is thrown, see whether it can be treated as
recoverable
(3) if so, mark the primary host as unavailable for a certain period
time and retry the request against the secondary host
(4) when the quarantine time elapses, mark the primary host as
potentially available
(5) repeat 

Hope this helps

Oleg


> On Mon, Mar 24, 2008 at 12:47 PM, Oleg Kalnichevski <ol...@apache.org>
> wrote:
> 
> >
> > On Thu, 2008-03-20 at 22:05 -0700, Ankur Shah wrote:
> > > I am using httpclient to connect to a remote server. Now for each
> > primary
> > > server there's also a backup failover server that i must talk to if
> > > connection attempts with the primary server fails. To achieve
> > > this what i'd ideally like to do is have HttpClient use a custom factory
> > > that tries to create a socket connection with the primary server and if
> > that
> > > fails (or times out), it'll go about  talking to backup server (and
> > retry a
> > > few times before giving up altogether).
> > >
> > > Now, searching on the web, i did find references to a
> > > MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory) that
> > is
> > > presumably in apache contrib package. But what that does is that in the
> > > overriden
> > > createSocket method, it takes host as a param (among other thing) and
> > then
> > > simply does a InetAddress.getAllByName which is not what i want. From
> > what i
> > > understand this caters to a DNS based failover which is not the case
> > here.
> > >
> > > What i'd ideally like to do is create a factory and have a createSocket
> > > method that takes primary and secondary host and port as args and then
> > tries
> > > both. I am not sure though how can i make HttpConnection class
> > > invoke my method.
> >
> > Ankur,
> >
> > What's the point of trying to implement the failover support on the
> > ProtocolSocketFactory level? What's wrong with just retrying failed
> > requests against a secondary server in case the primary one is
> > unavailable?
> >
> > Oleg
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >


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


Re: httpclient3 takes 3 minutes to catch IOException, why so long?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2008-03-24 at 16:44 -0700, ever friend wrote:
> Greeting...
>    
>   I use httpclient3 to communicate with a web server application.
>   
> when I test an error handling, it takes 3 minutes to catch the IOException (I manually unplug the internet connection). I assumed it takes couple ms to catch IOException since only one line of code/function-call there (client.executeMethod(method) to cause the IOException, but it takes 3 minutes. It seems sth. inside executeMethod is doing sth.........
>   
> Could some experters provide an inside about why it taks so long, and how to make it quicker to catch the exception?
>   
> (note: I have set the retryhandler to 0 retry. I just want to find the IO problem asap.)
>    
>   the code is sth. like:
> .....
> HttpClient client = new HttpClient();
> PostMethod method = new PostMethod("http://www.x.com");
>             
>             NameValuePair[] data = {
>               
>             };
>             method.setRequestBody(data);
>               
>             // do not want to retry, so set to 0.
>             method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
>               new DefaultHttpMethodRetryHandler(0, false));
>    
>               try {
>               // Execute the method.
>               int statusCode = client.executeMethod(method);
>                 if (statusCode != HttpStatus.SC_OK) {
>                  ....
>               }else{
>                 ...
>               }
>             } catch (HttpException e) {
>                 ...
>               
>             } catch (IOException e) {
>               ...
>             } finally {
>               // Release the connection.
>               method.releaseConnection();
>             } 
>    
>   Thank you,
>   Ever
> 

HttpClient simply propagates I/O exceptions thrown by the socket
input/output streams. This looks like a JRE/OS issue to me.

Oleg 


>        
> ---------------------------------
> Never miss a thing.   Make Yahoo your homepage.


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


httpclient3 takes 3 minutes to catch IOException, why so long?

Posted by ever friend <ev...@yahoo.com>.
 Greeting...
   
  I use httpclient3 to communicate with a web server application.
  
when I test an error handling, it takes 3 minutes to catch the IOException (I manually unplug the internet connection). I assumed it takes couple ms to catch IOException since only one line of code/function-call there (client.executeMethod(method) to cause the IOException, but it takes 3 minutes. It seems sth. inside executeMethod is doing sth.........
  
Could some experters provide an inside about why it taks so long, and how to make it quicker to catch the exception?
  
(note: I have set the retryhandler to 0 retry. I just want to find the IO problem asap.)
   
  the code is sth. like:
.....
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://www.x.com");
            
            NameValuePair[] data = {
              
            };
            method.setRequestBody(data);
              
            // do not want to retry, so set to 0.
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
              new DefaultHttpMethodRetryHandler(0, false));
   
              try {
              // Execute the method.
              int statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                 ....
              }else{
                ...
              }
            } catch (HttpException e) {
                ...
              
            } catch (IOException e) {
              ...
            } finally {
              // Release the connection.
              method.releaseConnection();
            } 
   
  Thank you,
  Ever

       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.

Re: Apache HttpClient - Using custom socket factory for failover support

Posted by Ankur Shah <an...@gmail.com>.
Hi Oleg,

Thanks for the response. The server that i am running (acting as a client in
this particular case) is working with multiple servers (hosting different
webservices) with each having a primary and a failover pair. From this
perspective the thought was that rather than having different classes (all
having their own business logic around invoking the service) handle the
connection failed attempt, somehow have some central code that attempts to
connect to the primary and fails over to the backup. I also wanted to make
sure that in case of a connection failure with primary, a new pool is
established against the backup server.

>From this perspective, an after thought that I had was this. I was going to
extend the MultiThreadedHttpConnectionManager and have a failover capable
connection manager. In there, i was thinking about exposing a method (or a
new constructor) that allows backup host and ports to be added. This manager
will cache a map of primary and backup server. In the overriden
getConnectionWithTimeout method, i'd have super return me a connection that
i'll check and make sure that is valid and if not, i'll go to my cache, get
the backup host config and open up a connection to that.

Let me know if you think this would be a good idea or not.

Ankur

On Mon, Mar 24, 2008 at 12:47 PM, Oleg Kalnichevski <ol...@apache.org>
wrote:

>
> On Thu, 2008-03-20 at 22:05 -0700, Ankur Shah wrote:
> > I am using httpclient to connect to a remote server. Now for each
> primary
> > server there's also a backup failover server that i must talk to if
> > connection attempts with the primary server fails. To achieve
> > this what i'd ideally like to do is have HttpClient use a custom factory
> > that tries to create a socket connection with the primary server and if
> that
> > fails (or times out), it'll go about  talking to backup server (and
> retry a
> > few times before giving up altogether).
> >
> > Now, searching on the web, i did find references to a
> > MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory) that
> is
> > presumably in apache contrib package. But what that does is that in the
> > overriden
> > createSocket method, it takes host as a param (among other thing) and
> then
> > simply does a InetAddress.getAllByName which is not what i want. From
> what i
> > understand this caters to a DNS based failover which is not the case
> here.
> >
> > What i'd ideally like to do is create a factory and have a createSocket
> > method that takes primary and secondary host and port as args and then
> tries
> > both. I am not sure though how can i make HttpConnection class
> > invoke my method.
>
> Ankur,
>
> What's the point of trying to implement the failover support on the
> ProtocolSocketFactory level? What's wrong with just retrying failed
> requests against a secondary server in case the primary one is
> unavailable?
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: Apache HttpClient - Using custom socket factory for failover support

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2008-03-20 at 22:05 -0700, Ankur Shah wrote:
> I am using httpclient to connect to a remote server. Now for each primary
> server there's also a backup failover server that i must talk to if
> connection attempts with the primary server fails. To achieve
> this what i'd ideally like to do is have HttpClient use a custom factory
> that tries to create a socket connection with the primary server and if that
> fails (or times out), it'll go about  talking to backup server (and retry a
> few times before giving up altogether).
> 
> Now, searching on the web, i did find references to a
> MultiHomeProtocolSocketFactory (that extends DefaultSocketFactory) that is
> presumably in apache contrib package. But what that does is that in the
> overriden
> createSocket method, it takes host as a param (among other thing) and then
> simply does a InetAddress.getAllByName which is not what i want. From what i
> understand this caters to a DNS based failover which is not the case here.
> 
> What i'd ideally like to do is create a factory and have a createSocket
> method that takes primary and secondary host and port as args and then tries
> both. I am not sure though how can i make HttpConnection class
> invoke my method.

Ankur,

What's the point of trying to implement the failover support on the
ProtocolSocketFactory level? What's wrong with just retrying failed
requests against a secondary server in case the primary one is
unavailable?  

Oleg


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