Example usage for io.vertx.core CompositeFuture all

List of usage examples for io.vertx.core CompositeFuture all

Introduction

In this page you can find the example usage for io.vertx.core CompositeFuture all.

Prototype

static <T1, T2, T3, T4> CompositeFuture all(Future<T1> f1, Future<T2> f2, Future<T3> f3, Future<T4> f4) 

Source Link

Document

Like #all(Future,Future) but with 4 futures.

Usage

From source file:org.eclipse.hono.application.HonoApplication.java

License:Open Source License

/**
 * Deploys all verticles the Hono server consists of.
 * /*from   w  w  w  .  j  av a 2s .  c  o m*/
 * @return true if deployment was successful
 */
private boolean registerVerticles() {

    if (vertx == null) {
        throw new IllegalStateException("no Vert.x instance has been configured");
    }

    final CountDownLatch startupLatch = new CountDownLatch(1);
    final AtomicBoolean startupSucceeded = new AtomicBoolean(false);

    // without creating a first instance here, deployment of the HonoServer verticles fails
    // TODO: find out why
    HonoServer firstInstance = serverFactory.getHonoServer();

    final int instanceCount = honoConfig.getMaxInstances();

    Future<Void> started = Future.future();
    started.setHandler(ar -> {
        if (ar.failed()) {
            LOG.error("cannot start up HonoServer", ar.cause());
        } else {
            startupSucceeded.set(true);
        }
        startupLatch.countDown();
    });

    CompositeFuture.all(deployAuthenticationService(), // we only need 1 authentication service
            deployAuthorizationService(), // we only need 1 authorization service
            deployCredentialsService(), deployRegistrationService()).setHandler(ar -> {
                if (ar.succeeded()) {
                    deployServer(firstInstance, instanceCount, started);
                } else {
                    started.fail(ar.cause());
                }
            });

    try {
        if (startupLatch.await(honoConfig.getStartupTimeout(), TimeUnit.SECONDS)) {
            if (startupSucceeded.get()) {
                LOG.info("Hono startup completed successfully");
            } else {
                shutdown();
            }
        } else {
            LOG.error("startup timed out after {} seconds, shutting down ...", honoConfig.getStartupTimeout());
            shutdown();
            startupSucceeded.set(false);
        }
    } catch (InterruptedException e) {
        LOG.error("startup process has been interrupted, shutting down ...");
        Thread.currentThread().interrupt();
        shutdown();
        startupSucceeded.set(false);
    }
    return startupSucceeded.get();
}