Example usage for io.vertx.core Future succeededFuture

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

Introduction

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

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:org.eclipse.hono.client.impl.HonoClientImpl.java

License:Open Source License

@Override
public HonoClient getOrCreateCredentialsClient(final String tenantId,
        final Handler<AsyncResult<CredentialsClient>> resultHandler) {
    final CredentialsClient credClient = activeCredClients.get(tenantId);
    if (credClient != null && credClient.isOpen()) {
        LOG.debug("reusing existing credentials client for [{}]", tenantId);
        resultHandler.handle(Future.succeededFuture(credClient));
    } else {// w  w w  .  j  av a  2  s . co m
        createCredentialsClient(tenantId, resultHandler);
    }
    return this;
}

From source file:org.eclipse.hono.client.impl.TelemetryConsumerImpl.java

License:Open Source License

/**
 * Creates a new telemetry data consumer for a tenant.
 * //w  w w .  j  a  v a 2  s . c om
 * @param context The vert.x context to run all interactions with the server on.
 * @param con The AMQP connection to the server.
 * @param tenantId The tenant to consumer events for.
 * @param pathSeparator The address path separator character used by the server.
 * @param prefetch the number of message credits the consumer grants and replenishes automatically as messages are
 *                 delivered. To manage credit manually, you can instead set prefetch to 0.
 * @param telemetryConsumer The consumer to invoke with each telemetry message received.
 * @param creationHandler The handler to invoke with the outcome of the creation attempt.
 * @throws NullPointerException if any of the parameters is {@code null}.
 */
public static void create(final Context context, final ProtonConnection con, final String tenantId,
        final String pathSeparator, final int prefetch, final Consumer<Message> telemetryConsumer,
        final Handler<AsyncResult<MessageConsumer>> creationHandler) {

    Objects.requireNonNull(context);
    Objects.requireNonNull(con);
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(pathSeparator);
    Objects.requireNonNull(telemetryConsumer);
    Objects.requireNonNull(creationHandler);

    createConsumer(context, con, tenantId, pathSeparator, TELEMETRY_ADDRESS_TEMPLATE, ProtonQoS.AT_MOST_ONCE,
            prefetch, (protonDelivery, message) -> telemetryConsumer.accept(message)).setHandler(created -> {
                if (created.succeeded()) {
                    creationHandler.handle(
                            Future.succeededFuture(new TelemetryConsumerImpl(context, created.result())));
                } else {
                    creationHandler.handle(Future.failedFuture(created.cause()));
                }
            });
}

From source file:org.eclipse.hono.client.impl.TelemetrySenderImpl.java

License:Open Source License

/**
 * Creates a new sender for publishing telemetry data to a Hono server.
 * //from w w  w.  ja  v a 2 s.  co m
 * @param context The vertx context to run all interactions with the server on.
 * @param con The connection to the Hono server.
 * @param tenantId The tenant that the telemetry data will be uploaded for.
 * @param deviceId The device that the telemetry data will be uploaded for or {@code null}
 *                 if the data to be uploaded will be produced by arbitrary devices of the
 *                 tenant.
 * @param closeHook The handler to invoke when the Hono server closes the sender. The sender's
 *                  target address is provided as an argument to the handler.
 * @param creationHandler The handler to invoke with the result of the creation attempt.
 * @throws NullPointerException if any of context, connection, tenant or handler is {@code null}.
 */
public static void create(final Context context, final ProtonConnection con, final String tenantId,
        final String deviceId, final Handler<String> closeHook,
        final Handler<AsyncResult<MessageSender>> creationHandler) {

    Objects.requireNonNull(context);
    Objects.requireNonNull(con);
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(creationHandler);

    final String targetAddress = getTargetAddress(tenantId, deviceId);
    createSender(context, con, targetAddress, ProtonQoS.AT_MOST_ONCE, closeHook).setHandler(created -> {
        if (created.succeeded()) {
            creationHandler.handle(Future.succeededFuture(
                    new TelemetrySenderImpl(created.result(), tenantId, targetAddress, context, closeHook)));
        } else {
            creationHandler.handle(Future.failedFuture(created.cause()));
        }
    });
}

From source file:org.eclipse.hono.client.impl.TenantClientImpl.java

License:Open Source License

/**
 * Creates a new tenant client./* www. ja va2  s.c o  m*/
 *
 * @param context The vert.x context to run all interactions with the server on.
 * @param clientConfig The configuration properties to use.
 * @param cacheProvider A factory for cache instances for tenant configuration results. If {@code null}
 *                     the client will not cache any results from the Tenant service.
 * @param con The AMQP connection to the server.
 * @param senderCloseHook A handler to invoke if the peer closes the sender link unexpectedly.
 * @param receiverCloseHook A handler to invoke if the peer closes the receiver link unexpectedly.
 * @param creationHandler The handler to invoke with the outcome of the creation attempt.
 * @throws NullPointerException if any of the parameters, except for senderCloseHook and receiverCloseHook, is {@code null}.
 */
public final static void create(final Context context, final ClientConfigProperties clientConfig,
        final CacheProvider cacheProvider, final ProtonConnection con, final Handler<String> senderCloseHook,
        final Handler<String> receiverCloseHook, final Handler<AsyncResult<TenantClient>> creationHandler) {

    LOG.debug("creating new tenant client");
    final TenantClientImpl client = new TenantClientImpl(context, clientConfig);
    if (cacheProvider != null) {
        client.setResponseCache(cacheProvider.getCache(TenantClientImpl.getTargetAddress()));
    }
    client.createLinks(con, senderCloseHook, receiverCloseHook).setHandler(s -> {
        if (s.succeeded()) {
            LOG.debug("successfully created tenant client");
            creationHandler.handle(Future.succeededFuture(client));
        } else {
            LOG.debug("failed to create tenant client", s.cause());
            creationHandler.handle(Future.failedFuture(s.cause()));
        }
    });
}

From source file:org.eclipse.hono.connection.ConnectionFactoryImpl.java

License:Open Source License

private void handleConnectionAttemptResult(final AsyncResult<ProtonConnection> conAttempt,
        final ProtonClientOptions clientOptions, final Handler<AsyncResult<ProtonConnection>> closeHandler,
        final Handler<ProtonConnection> disconnectHandler,
        final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {

    if (conAttempt.failed()) {

        logger.debug("can't connect to AMQP 1.0 container [{}://{}:{}]: {}",
                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(),
                conAttempt.cause().getMessage());
        connectionResultHandler.handle(Future.failedFuture(conAttempt.cause()));

    } else {//from  w  w w. ja v a 2  s  . c o  m

        // at this point the SASL exchange has completed successfully
        logger.debug("connected to AMQP 1.0 container [{}://{}:{}], opening connection ...",
                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
        ProtonConnection downstreamConnection = conAttempt.result();
        downstreamConnection.setContainer(String.format("%s-%s", config.getName(), UUID.randomUUID()))
                .setHostname(config.getAmqpHostname()).openHandler(openCon -> {
                    if (openCon.succeeded()) {
                        logger.debug("connection to container [{}] at [{}://{}:{}] open",
                                downstreamConnection.getRemoteContainer(),
                                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
                        downstreamConnection.disconnectHandler(disconnectHandler);
                        downstreamConnection.closeHandler(closeHandler);
                        connectionResultHandler.handle(Future.succeededFuture(downstreamConnection));
                    } else {
                        logger.warn("can't open connection to container [{}] at [{}://{}:{}]",
                                downstreamConnection.getRemoteContainer(),
                                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(),
                                openCon.cause());
                        connectionResultHandler.handle(Future.failedFuture(openCon.cause()));
                    }
                }).open();
    }
}

From source file:org.eclipse.hono.connection.impl.ConnectionFactoryImpl.java

License:Open Source License

private void handleConnectionAttemptResult(final AsyncResult<ProtonConnection> conAttempt,
        final ProtonClientOptions clientOptions, final Handler<AsyncResult<ProtonConnection>> closeHandler,
        final Handler<ProtonConnection> disconnectHandler,
        final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {

    if (conAttempt.failed()) {

        logger.debug("can't connect to AMQP 1.0 container [{}://{}:{}]: {}",
                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(),
                conAttempt.cause().getMessage());
        connectionResultHandler.handle(Future.failedFuture(conAttempt.cause()));

    } else {/*  w ww.  j  av  a  2s  . c om*/

        // at this point the SASL exchange has completed successfully
        logger.debug("connected to AMQP 1.0 container [{}://{}:{}], opening connection ...",
                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
        final ProtonConnection downstreamConnection = conAttempt.result();
        downstreamConnection.setContainer(String.format("%s-%s", config.getName(), UUID.randomUUID()))
                .setHostname(config.getAmqpHostname()).openHandler(openCon -> {
                    if (openCon.succeeded()) {
                        logger.debug("connection to container [{}] at [{}://{}:{}] open",
                                downstreamConnection.getRemoteContainer(),
                                clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
                        downstreamConnection.disconnectHandler(disconnectHandler);
                        downstreamConnection.closeHandler(closeHandler);
                        connectionResultHandler.handle(Future.succeededFuture(downstreamConnection));
                    } else {
                        final ErrorCondition error = downstreamConnection.getRemoteCondition();
                        if (error == null) {
                            logger.warn("can't open connection to container [{}] at [{}://{}:{}]",
                                    downstreamConnection.getRemoteContainer(),
                                    clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(),
                                    config.getPort(), openCon.cause());
                        } else {
                            logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {} -{}",
                                    downstreamConnection.getRemoteContainer(),
                                    clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(),
                                    config.getPort(), error.getCondition(), error.getDescription());
                        }
                        connectionResultHandler.handle(Future.failedFuture(openCon.cause()));
                    }
                }).disconnectHandler(disconnectedCon -> {
                    logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {}",
                            downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp",
                            config.getHost(), config.getPort(),
                            "underlying connection was disconnected while opening AMQP connection");
                    connectionResultHandler.handle(Future.failedFuture(
                            "underlying connection was disconnected while opening AMQP connection"));
                }).open();
    }
}

From source file:org.eclipse.hono.deviceregistry.FileBasedCredentialsService.java

License:Open Source License

@Override
public final void getCredentials(final String tenantId, final String type, final String authId,
        final Handler<AsyncResult<CredentialsResult<JsonObject>>> resultHandler) {
    CredentialsResult<JsonObject> credentialsResult = getCredentialsResult(tenantId, authId, type);
    resultHandler.handle(Future.succeededFuture(credentialsResult));
}

From source file:org.eclipse.hono.deviceregistry.FileBasedRegistrationService.java

License:Open Source License

@Override
public void getDevice(final String tenantId, final String deviceId,
        final Handler<AsyncResult<RegistrationResult>> resultHandler) {
    resultHandler.handle(Future.succeededFuture(getDevice(tenantId, deviceId)));
}

From source file:org.eclipse.hono.deviceregistry.FileBasedRegistrationService.java

License:Open Source License

@Override
public void removeDevice(final String tenantId, final String deviceId,
        final Handler<AsyncResult<RegistrationResult>> resultHandler) {

    resultHandler.handle(Future.succeededFuture(removeDevice(tenantId, deviceId)));
}

From source file:org.eclipse.hono.deviceregistry.FileBasedRegistrationService.java

License:Open Source License

@Override
public void addDevice(final String tenantId, final String deviceId, final JsonObject data,
        final Handler<AsyncResult<RegistrationResult>> resultHandler) {

    resultHandler.handle(Future.succeededFuture(addDevice(tenantId, deviceId, data)));
}