Search in sources :

Example 1 with CircuitBreaker

use of io.vertx.circuitbreaker.CircuitBreaker in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() {
    CircuitBreakerOptions options = new CircuitBreakerOptions().setMaxFailures(5).setTimeout(5000).setFallbackOnFailure(true);
    CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", vertx, options).openHandler(v -> {
        System.out.println("Circuit opened");
    }).closeHandler(v -> {
        System.out.println("Circuit closed");
    });
    Future<String> result = breaker.executeWithFallback(future -> {
        vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {
            if (response.statusCode() != 200) {
                future.fail("HTTP error");
            } else {
                response.exceptionHandler(future::fail).bodyHandler(buffer -> {
                    future.complete(buffer.toString());
                });
            }
        });
    }, v -> {
        // Executed when the circuit is opened
        return "Hello (fallback)";
    });
    result.setHandler(ar -> {
        // Do something with the result
        System.out.println("Result: " + ar.result());
    });
}
Also used : CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions) Launcher(io.vertx.core.Launcher) AbstractVerticle(io.vertx.core.AbstractVerticle) CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker) Future(io.vertx.core.Future) CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker) CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions)

Aggregations

CircuitBreaker (io.vertx.circuitbreaker.CircuitBreaker)1 CircuitBreakerOptions (io.vertx.circuitbreaker.CircuitBreakerOptions)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Future (io.vertx.core.Future)1 Launcher (io.vertx.core.Launcher)1