Example usage for io.vertx.core Future future

List of usage examples for io.vertx.core Future future

Introduction

In this page you can find the example usage for io.vertx.core Future future.

Prototype

future

Source Link

Usage

From source file:org.eclipse.hono.server.SenderFactoryImpl.java

License:Open Source License

private Future<ProtonSender> newSender(final ProtonConnection connection, final ProtonSession session,
        final String address, final ProtonQoS qos, final Handler<ProtonSender> sendQueueDrainHandler) {

    Future<ProtonSender> result = Future.future();
    ProtonSender sender = session.createSender(address);
    sender.setQoS(qos);/*  w  w  w .  ja va  2  s  .com*/
    sender.sendQueueDrainHandler(sendQueueDrainHandler);
    sender.openHandler(openAttempt -> {
        if (openAttempt.succeeded()) {
            LOG.debug("sender [{}] for container [{}] open", address, connection.getRemoteContainer());
            result.complete(openAttempt.result());
        } else {
            LOG.debug("could not open sender [{}] for container [{}]", address, connection.getRemoteContainer(),
                    openAttempt.cause());
            result.fail(openAttempt.cause());
        }
    });
    sender.closeHandler(closed -> {
        if (closed.succeeded()) {
            LOG.debug("sender [{}] for container [{}] closed", address, connection.getRemoteContainer());
        }
    });
    sender.open();
    return result;
}

From source file:org.eclipse.hono.service.AbstractApplication.java

License:Open Source License

/**
 * Starts up this application./*from w ww . j  a  va  2s  .  com*/
 * <p>
 * The start up process entails the following steps:
 * <ol>
 * <li>invoke <em>deployRequiredVerticles</em> to deploy the verticle(s) implementing the service's functionality</li>
 * <li>invoke <em>deployServiceVerticles</em> to deploy the protocol specific service endpoints</li>
 * <li>invoke <em>postRegisterServiceVerticles</em> to perform any additional post deployment steps</li>
 * <li>start the health check server</li>
 * </ol>
 * 
 * @param args The command line arguments provided to the application.
 */
public void run(final ApplicationArguments args) {

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

    healthCheckServer = new HealthCheckServer(vertx, config);

    final CountDownLatch startupLatch = new CountDownLatch(1);
    final int startupTimeoutSeconds = config.getStartupTimeout();

    Future<Void> started = Future.future();
    started.setHandler(s -> {
        if (s.failed()) {
            log.error("cannot start up application", s.cause());
        } else {
            startupLatch.countDown();
        }
    });

    deployRequiredVerticles(config.getMaxInstances()).compose(s -> deployServiceVerticles())
            .compose(s -> postRegisterServiceVerticles()).compose(s -> healthCheckServer.start())
            .compose(s -> started.complete(), started);

    try {
        if (startupLatch.await(startupTimeoutSeconds, TimeUnit.SECONDS)) {
            log.info("application startup completed successfully");
        } else {
            log.error("startup timed out after {} seconds, shutting down ...", startupTimeoutSeconds);
            shutdown();
        }
    } catch (InterruptedException e) {
        log.error("startup process has been interrupted, shutting down ...");
        Thread.currentThread().interrupt();
        shutdown();
    }
}

From source file:org.eclipse.hono.service.AbstractApplication.java

License:Open Source License

private CompositeFuture deployServiceVerticles() {
    final int maxInstances = config.getMaxInstances();

    @SuppressWarnings("rawtypes")
    final List<Future> deploymentTracker = new ArrayList<>();

    for (ObjectFactory<? extends AbstractServiceBase<?>> serviceFactory : serviceFactories) {

        AbstractServiceBase<?> serviceInstance = serviceFactory.getObject();

        healthCheckServer.registerHealthCheckResources(serviceInstance);

        final Future<String> deployTracker = Future.future();
        vertx.deployVerticle(serviceInstance, deployTracker.completer());
        deploymentTracker.add(deployTracker);

        for (int i = 1; i < maxInstances; i++) { // first instance has already been deployed
            serviceInstance = serviceFactory.getObject();
            log.debug("created new instance of service: {}", serviceInstance);
            final Future<String> tracker = Future.future();
            vertx.deployVerticle(serviceInstance, tracker.completer());
            deploymentTracker.add(tracker);
        }// w w  w. j  ava  2  s  .c om
    }

    return CompositeFuture.all(deploymentTracker);
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

@Override
public final Future<Void> startInternal() {
    Future<Void> result = Future.future();
    if (messaging == null) {
        result.fail("Hono Messaging client must be set");
    } else if (registration == null) {
        result.fail("Device Registration client must be set");
    } else {//w  ww.j a  va  2s.  co  m
        if (credentials == null) {
            LOG.info("Credentials client not configured, using Device Registration client instead.");
        }
        doStart(result);
    }
    return result;
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

@Override
public final Future<Void> stopInternal() {
    Future<Void> result = Future.future();
    doStop(result);
    return result;
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Closes the connections to the Hono Messaging component and the Device Registration service.
 * /* w w  w.  j  a  va  2  s . c  om*/
 * @param closeHandler The handler to notify about the result.
 */
protected final void closeClients(final Handler<AsyncResult<Void>> closeHandler) {

    Future<Void> messagingTracker = Future.future();
    Future<Void> registrationTracker = Future.future();
    Future<Void> credentialsTracker = Future.future();

    if (messaging == null) {
        messagingTracker.complete();
    } else {
        messaging.shutdown(messagingTracker.completer());
    }

    if (registration == null) {
        registrationTracker.complete();
    } else {
        registration.shutdown(registrationTracker.completer());
    }

    if (credentials == null) {
        credentialsTracker.complete();
    } else {
        credentials.shutdown(credentialsTracker.completer());
    }

    CompositeFuture.all(messagingTracker, registrationTracker, credentialsTracker).setHandler(s -> {
        if (closeHandler != null) {
            if (s.succeeded()) {
                closeHandler.handle(Future.succeededFuture());
            } else {
                closeHandler.handle(Future.failedFuture(s.cause()));
            }
        }
    });
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Gets a client for sending telemetry data for a tenant.
 * //from  w  w  w.  ja v  a  2 s  .c  o  m
 * @param tenantId The tenant to send the telemetry data for.
 * @return The client.
 */
protected final Future<MessageSender> getTelemetrySender(final String tenantId) {
    Future<MessageSender> result = Future.future();
    messaging.getOrCreateTelemetrySender(tenantId, result.completer());
    return result;
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Gets a client for sending events for a tenant.
 * /*from   w w w  .  j  a  v a 2  s. c  om*/
 * @param tenantId The tenant to send the events for.
 * @return The client.
 */
protected final Future<MessageSender> getEventSender(final String tenantId) {
    Future<MessageSender> result = Future.future();
    messaging.getOrCreateEventSender(tenantId, result.completer());
    return result;
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Gets a client for interacting with the Device Registration service.
 * /*from  w  w w. ja  va2 s  . c  om*/
 * @param tenantId The tenant that the client is scoped to.
 * @return The client.
 */
protected final Future<RegistrationClient> getRegistrationClient(final String tenantId) {
    Future<RegistrationClient> result = Future.future();
    getRegistrationServiceClient().getOrCreateRegistrationClient(tenantId, result.completer());
    return result;
}

From source file:org.eclipse.hono.service.AbstractProtocolAdapterBase.java

License:Open Source License

/**
 * Gets a client for interacting with the Credentials service.
 *
 * @param tenantId The tenant that the client is scoped to.
 * @return The client./*from  w w w  . j ava2  s  .c  o m*/
 */
protected final Future<CredentialsClient> getCredentialsClient(final String tenantId) {
    Future<CredentialsClient> result = Future.future();
    getCredentialsServiceClient().getOrCreateCredentialsClient(tenantId, result.completer());
    return result;
}