7

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 Promises 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 Futures. 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+?

Daniel Kesner
  • 226
  • 4
  • 16

1 Answers1

8

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);
    }
  });
}
tsegismont
  • 8,591
  • 1
  • 17
  • 27
  • 1
    is it possible to use `vertx.deployVerticle(new SomeVerticle(), v1Promise)`? It would be nice for fluency – injecteer Oct 07 '19 at 09:23
  • 1
    Sure, if you change v1Promise type to `Promise`. I kept the original code here to focus on the question asked. – tsegismont Oct 07 '19 at 12:29
  • This might be more obvious with Vert.x 4 (currently in milestones) that provides methods returning `Future` as well, so the example above looks more streamlined. – Julien Viet Jan 02 '20 at 15:27