Reputation: 839
The HTTP commons client 4.12 tutorial section on exception handling clearly shows that one should be able to set a request retry handler by doing the following...
httpclient.setHttpRequestRetryHandler(myRetryHandler);
in eclipse I tried that and it reports HttpClient has no such method. It suggests i cast the client to AbstractHttpClient
then call .setHttpRequestRetryHandler
like so:
((AbstractHttpClient) httpclient).setHttpRequestRetryHandler(myRetryHandler);
then my code works, but this discrepancy between documentation and API makes me wonder if I'm doing something I shouldn't.
Is the documentation wrong or is it me?
Upvotes: 3
Views: 8655
Reputation: 89
The proper way to use HttpClient in this case is to use AutoRetryHttpClient, which accepts a DefaultServiceUnavailableRetryStrategy. One possible reason, why this is often overlooked is that AutoRetryHttpClient implements HttpClient interface instead of extending AbstractHttpClient. No need for casting in this case.
DefaultServiceUnavailableRetryStrategy retryStrategy = new DefaultServiceUnavailableRetryStrategy(5, 50);
AutoRetryHttpClient httpClient = new AutoRetryHttpClient(retryStrategy);
Upvotes: 3
Reputation: 10007
You're totally correct. The documentation uses a concrete DefaultHttpClient
(which is a subclass of AbstractHttpClient
and so has the setHttpRequestRetryHandler()
method.
As you are doing the right thing and programming to the HttpClient
interface (which sadly doesn't expose that method), you have to do that nasty cast.
It looks like the Apache team have decided to keep the HttpClient
interface super-clean, at the expense of client code :-(
Upvotes: 4