An extension to apache httpclient that adds support for failover on multiple http hosts.
<repositories>
<repository>
<id>mcaprari-releases</id>
<url>https://github.com/mcaprari/mcaprari-maven-repo/raw/master/releases</url>
</repository>
<repository>
<id>mcaprari-snapshots</id>
<url>https://github.com/mcaprari/mcaprari-maven-repo/raw/master/snapshots</url>
</repository>
</repositories>
<dependency>
<groupId>httpfailover</groupId>
<artifactId>httpclient-failover</artifactId>
<version>1.0</version>
</dependency>
FailoverHttpClient extends DefaultHttpClient adding few methods:
/**
* Tries to execute the request on all targets.
* Each target failure is evaluated using the multiTargetRetryHandler.
*
* In case of non-retriable failure, the last exception is thrown.
*
* @param targets the candidate target hosts for the request.
* The request is executed on each hosts until one succeeds.
* @param request the request to execute
* @param context the request-specific execution context,
* or <code>null</code> to use a default context
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public HttpResponse execute(List<HttpHost> targets, HttpRequest request, HttpContext context)
public HttpResponse execute(List<HttpHost> targets, HttpRequest request)
/**
* Executes a request using the default context and processes the
* response using the given response handler.
*
* @param targets the candidate target hosts for the request.
* The request is executed on each hosts until one succeeds.
* @param request the request to execute
* @param responseHandler the response handler
* @param context the context to use for the execution, or
* <code>null</code> to use the default context
*
* @return the response object as generated by the response handler.
* @throws IOException in case of a problem or the connection was aborted
* @throws ClientProtocolException in case of an http protocol error
*/
public <T> T execute(List<HttpHost> targets, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
public <T> T execute(List<HttpHost> targets, HttpRequest request, ResponseHandler<? extends T> responseHandler)
/**
* Set a for determining if an HttpRequest should fail over a different host
* @param handler the handler
*/
void setMultiTargetRetryHandler(FailoverRetryHandler handler)
// create a client instance
FailoverHttpClient failoverHttpClient = new FailoverHttpClient();
// the list of hosts
List<HttpHost> hosts = Arrays.asList(
new HttpHost("localhost", 9090),
new HttpHost("localhost", 9191)
);
// the request
HttpGet request = new HttpGet(URI.create("/file.txt"));
HttpResponse response = failoverHttpClient.execute(hosts, request);