I have a Vert.x 3.7.1 method that deploys a bunch of verticles and if all of the deployments succeed, sends a message through the event bus that does some startup work. The structure of the method looks like this:
void deploy() {
Future<Void> v1Future = Future.future();
Future<Void> v2Future = Future.future();
// ...
vertx.deployVerticle(new SomeVerticle(), result -> {
if (result.succeeded()) {
v1Future.complete();
} else {
v1Future.fail(result.cause());
}
});
// ...
List<Future<Void>> allFutures = ImmutableList.of(v1Future, v2Future);
CompositeFuture.all(allFutures).setHandler(result -> {
if (result.succeeded()) {
vertx.eventBus().send("some-address");
}
});
}
I want to replicate this same functionality with Promise
s in Vert.x 3.8.1+, since Future.future()
and most of the associated methods are now deprecated. The problem is, there's no CompositePromise
or anything that seems similar to Future
s. How can I execute a series of deployments and then if and only if all deployments succeed, do something else using the new Promise
class in Vert.x 3.8.1+?
CompositeFuture
still exists and it will not be replaced. The reason is that what needs to be composed are the asynchronous results, not the promises.
Here is how you can compose the futures corresponding to each promise:
void deploy() {
Promise<Void> v1Promise = Promise.promise();
Promise<Void> v2Promise = Promise.promise();
// ...
vertx.deployVerticle(new SomeVerticle(), result -> {
if (result.succeeded()) {
v1Promise.complete();
} else {
v1Promise.fail(result.cause());
}
});
// ...
List<Future> allFutures = ImmutableList.of(v1Promise.future(), v2Promise.future());
CompositeFuture.all(allFutures).setHandler(result -> {
if (result.succeeded()) {
vertx.eventBus().send("some-address", message);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With