Skip to content

Commit

Permalink
Modernize circuit breaker examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 7, 2024
1 parent 695b787 commit ed1e84d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,56 @@
import io.vertx.circuitbreaker.CircuitBreakerOptions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientAgent;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpResponseExpectation;
import io.vertx.launcher.application.VertxApplication;

/**
* @author <a href="[email protected]">Pahan</a>
*/

public class Client extends AbstractVerticle {
public class Client extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private HttpClientAgent httpClient;
private CircuitBreaker breaker;

@Override
public void start() {
public Future<?> start() {
CircuitBreakerOptions options = new CircuitBreakerOptions()
.setMaxFailures(5)
.setTimeout(5000)
.setFallbackOnFailure(true);

CircuitBreaker breaker =
httpClient = vertx.createHttpClient();
breaker =
CircuitBreaker.create("my-circuit-breaker", vertx, options)
.openHandler(v -> {
System.out.println("Circuit opened");
}).closeHandler(v -> {
System.out.println("Circuit closed");
});

breaker.executeWithFallback(promise -> {
vertx.createHttpClient().request(HttpMethod.GET, 8080, "localhost", "/").compose(req -> {
return req.send().compose(resp -> {
if (resp.statusCode() != 200) {
return Future.failedFuture("HTTP error");
} else {
return resp.body().map(Buffer::toString);
}
System.out.println("Circuit closed");
});
}).onComplete(promise);
}, v -> {
// Executed when the circuit is opened
return "Hello (fallback)";
}).onComplete(ar -> {
// Do something with the result
System.out.println("Result: " + ar.result());
});

return breaker
.executeWithFallback(() -> httpClient
.request(HttpMethod.GET, 8080, "localhost", "/").
compose(req -> req
.send()
.expecting(HttpResponseExpectation.SC_OK)
.compose(resp -> resp.body())
.map(body -> body.toString())), v -> {
// Executed when the circuit is opened
return "Hello (fallback)";
})
.onSuccess(res -> {
// Do something with the result
System.out.println("Result: " + res);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.vertx.example.circuit.breaker;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;

public class Server extends AbstractVerticle {
public class Server extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}

@Override
public void start() {
vertx.createHttpServer()
public Future<?> start() {
return vertx
.createHttpServer()
.requestHandler(req -> req.response().end("Bonjour"))
.listen(8080);
}
Expand Down

0 comments on commit ed1e84d

Please sign in to comment.