users@jersey.java.net

[Jersey] Re: Connection Retry on jersey 1.5 Client

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Tue, 15 Feb 2011 14:56:44 +0100

Hi again,

sure, it is fairly simple - you should improve this (add some checks
etc), but it is usable:

         Client c = client();

         c.addFilter(new ClientFilter() {
             private final int maxRetries = 3;

             @Override
             public ClientResponse handle(ClientRequest cr) throws
ClientHandlerException {

                 int i = 0;

                 while(i < maxRetries) {

                     i++;

                     try {
                         return getNext().handle(cr);
                     } catch (ClientHandlerException e) {

                         // you migh want t check e.getClause, i.e.
something like
                         // if(e.getClause() instanceof
ConnectException) { ... } else { throw e; }
                         Logger.getLogger("myFilter").log(Level.INFO,
"Connection Timeout (" + cr.getURI() + "), will try " + (maxRetries - i)
+ " more time(s).", e);
                     }
                 }
                 throw new ClientHandlerException("Connection retries
limit exceeded.");
             };
         });

         c.resource("http://oracle.com").get(String.class);


I guess you would like to extract this to standard class and put
connection retries as a parameter to constructor.. or anything else -
depends on your requirements.

Hope it helps.

Regards,
Pavel

On 02/09/2011 08:49 PM, SISE Inc. wrote:
>
> Hello Pavel,
>
> The ReadTimeout have a ClientConfig property that I set on ClientRequest.
>
> I would appreciate any tip you can give on this filter... I'm only
> using the Jersey Client API. Just a newbie on this topic.. Though the
> app works perfectly now, we just want a way to retry dropped connection..
>
> Regards,
>
> Tim
> SISENT
>
> *From:* Pavel Bucek [mailto:pavel.bucek_at_oracle.com]
> *Sent:* Monday, February 07, 2011 7:11 AM
> *To:* users_at_jersey.java.net
> *Subject:* [Jersey] Re: Connection Retry on jersey 1.5 Client
>
> Hello Sisent,
>
> this is not "standard" http option in Java SE, but you can easily
> implement it by yourself, similarly to ReadTimeOut filter you are
> using. There is no limit to what you can do in filter, for example you
> can do multiple subsequent requests (which is almost what you need),
> see HTTPDigestAuthFilter.handle().
>
> And, if you want, you can file and RFE afterwards, include your patch
> and filter you implemented can be included in next release.
>
> Pavel
>
> On 02/04/2011 11:43 PM, SISE Inc. wrote:
>
> Hi,
>
> How do I set the number of Connection retry on the Client if I have
> the following code to get my WebResource?
>
> ClientConfig cc = *new* DefaultClientConfig();
>
> Client c = Client./create/(cc);
>
> c.addFilter(*new* LoggingFilter()); //Login Filter
>
> resource = c.resource(buri);
>
> resource.addFilter(*new* ReadTimeOut(timeOut)); // Time out filter
> for Timeout
>
> The ClientConfig does not have any option for Connection Retry... Any
> help on this?
>
> Regards,
>
> sisent
>