-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
33 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}); | ||
} | ||
} |
10 changes: 6 additions & 4 deletions
10
circuit-breaker-examples/src/main/java/io/vertx/example/circuit/breaker/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters