Giter Site home page Giter Site logo

niuqinghua / httpclient-failover Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caprazzo/httpclient-failover

0.0 1.0 0.0 174 KB

An extension to apache httpclient that adds support for failover on multiple http hosts.

License: Apache License 2.0

Java 100.00%

httpclient-failover's Introduction

httpclient-failover

An extension to apache httpclient that adds support for failover on multiple http hosts.

Using from maven

<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)

Example Usage

// 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);

Bitdeli Badge

httpclient-failover's People

Contributors

caprazzo avatar bitdeli-chef avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.