What's the Promise equivalent of CompositeFuture in Vert.x 3.8.1+?
Asked Answered
K

1

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+?

Kinfolk answered 4/10, 2019 at 18:4 Comment(0)
A
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);
    }
  });
}
Awoke answered 7/10, 2019 at 7:51 Comment(3)
is it possible to use vertx.deployVerticle(new SomeVerticle(), v1Promise)? It would be nice for fluencyWaldo
Sure, if you change v1Promise type to Promise<String>. I kept the original code here to focus on the question asked.Awoke
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.Gadgeteer

© 2022 - 2024 — McMap. All rights reserved.