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() 

Source Link

Document

Create a succeeded future with a null result

Usage

From source file:org.eclipse.hono.adapter.mqtt.AbstractVertxBasedMqttProtocolAdapter.java

License:Open Source License

private Future<Void> bindSecureMqttServer() {

    if (isSecurePortEnabled()) {
        final MqttServerOptions options = new MqttServerOptions();
        options.setHost(getConfig().getBindAddress()).setPort(determineSecurePort())
                .setMaxMessageSize(getConfig().getMaxPayloadSize());
        addTlsKeyCertOptions(options);/*from   w  w w.  j a  v a2  s  .  com*/
        addTlsTrustOptions(options);

        return bindMqttServer(options, server).map(s -> {
            server = s;
            return (Void) null;
        }).recover(t -> {
            return Future.failedFuture(t);
        });
    } else {
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.adapter.mqtt.AbstractVertxBasedMqttProtocolAdapter.java

License:Open Source License

private Future<Void> bindInsecureMqttServer() {

    if (isInsecurePortEnabled()) {
        final MqttServerOptions options = new MqttServerOptions();
        options.setHost(getConfig().getInsecurePortBindAddress()).setPort(determineInsecurePort())
                .setMaxMessageSize(getConfig().getMaxPayloadSize());

        return bindMqttServer(options, insecureServer).map(server -> {
            insecureServer = server;/*from w ww  .j  a v  a 2 s .c  o m*/
            return (Void) null;
        }).recover(t -> {
            return Future.failedFuture(t);
        });
    } else {
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.adapter.mqtt.VertxBasedMqttProtocolAdapter.java

License:Open Source License

private Future<MqttServer> bindSecureMqttServer() {

    if (isSecurePortEnabled()) {
        MqttServerOptions options = new MqttServerOptions();
        options.setHost(getConfig().getBindAddress()).setPort(determineSecurePort())
                .setMaxMessageSize(getConfig().getMaxPayloadSize());
        addTlsKeyCertOptions(options);/*from w  w w . ja v  a 2 s  .co  m*/
        addTlsTrustOptions(options);

        Future<MqttServer> result = Future.future();
        result.setHandler(mqttServerAsyncResult -> {
            server = mqttServerAsyncResult.result();
        });
        bindMqttServer(options, server, result);
        return result;
    } else {
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.adapter.mqtt.VertxBasedMqttProtocolAdapter.java

License:Open Source License

private Future<MqttServer> bindInsecureMqttServer() {

    if (isInsecurePortEnabled()) {
        MqttServerOptions options = new MqttServerOptions();
        options.setHost(getConfig().getInsecurePortBindAddress()).setPort(determineInsecurePort())
                .setMaxMessageSize(getConfig().getMaxPayloadSize());

        Future<MqttServer> result = Future.future();
        result.setHandler(mqttServerAsyncResult -> {
            insecureServer = mqttServerAsyncResult.result();
        });/*from  ww w .  ja  v  a  2 s. c  om*/
        bindMqttServer(options, insecureServer, result);
        return result;
    } else {
        return Future.succeededFuture();
    }
}

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

License:Open Source License

/**
 * Closes this client's sender and receiver links to Hono.
 * //w  w  w  . j a  v  a2  s  .  c  o m
 * @param closeHandler the handler to be notified about the outcome.
 * @throws NullPointerException if the given handler is {@code null}.
 */
protected void closeLinks(final Handler<AsyncResult<Void>> closeHandler) {

    Objects.requireNonNull(closeHandler);

    final Future<ProtonSender> senderCloseHandler = Future.future();
    final Future<ProtonReceiver> receiverCloseHandler = Future.future();
    receiverCloseHandler.setHandler(closeAttempt -> {
        if (closeAttempt.succeeded()) {
            closeHandler.handle(Future.succeededFuture());
        } else {
            closeHandler.handle(Future.failedFuture(closeAttempt.cause()));
        }
    });

    senderCloseHandler.compose(closedSender -> {
        if (receiver != null && receiver.isOpen()) {
            receiver.closeHandler(closeAttempt -> {
                LOG.debug("closed message consumer for [{}]", receiver.getSource().getAddress());
                receiverCloseHandler.complete(receiver);
            }).close();
        } else {
            receiverCloseHandler.complete();
        }
    }, receiverCloseHandler);

    context.runOnContext(close -> {

        if (sender != null && sender.isOpen()) {
            sender.closeHandler(closeAttempt -> {
                LOG.debug("closed message sender for [{}]", sender.getTarget().getAddress());
                senderCloseHandler.complete(sender);
            }).close();
        } else {
            senderCloseHandler.complete();
        }
    });
}

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

License:Open Source License

@Override
public void shutdown(final Handler<AsyncResult<Void>> completionHandler) {

    if (connection == null || connection.isDisconnected()) {
        LOG.info("connection to server [{}:{}] already closed", connectionFactory.getHost(),
                connectionFactory.getPort());
        completionHandler.handle(Future.succeededFuture());
    } else {//from  w ww. jav  a 2s. c  o  m
        context.runOnContext(close -> {
            LOG.info("closing connection to server [{}:{}]...", connectionFactory.getHost(),
                    connectionFactory.getPort());
            connection.disconnectHandler(null); // make sure we are not trying to re-connect
            connection.closeHandler(closedCon -> {
                if (closedCon.succeeded()) {
                    LOG.info("closed connection to server [{}:{}]", connectionFactory.getHost(),
                            connectionFactory.getPort());
                } else {
                    LOG.info("could not close connection to server [{}:{}]", connectionFactory.getHost(),
                            connectionFactory.getPort(), closedCon.cause());
                }
                connection.disconnect();
                if (completionHandler != null) {
                    completionHandler.handle(Future.succeededFuture());
                }
            }).close();
        });
    }
}

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

License:Open Source License

/**
 * Registers any additional health checks that the service implementation components provide.
 * /*from   ww w .  j  a va 2s  .c o m*/
 * @return A succeeded future.
 */
@Override
protected Future<Void> postRegisterServiceVerticles() {
    if (HealthCheckProvider.class.isInstance(authenticationService)) {
        registerHealthchecks((HealthCheckProvider) authenticationService);
    }
    if (HealthCheckProvider.class.isInstance(credentialsService)) {
        registerHealthchecks((HealthCheckProvider) credentialsService);
    }
    if (HealthCheckProvider.class.isInstance(registrationService)) {
        registerHealthchecks((HealthCheckProvider) registrationService);
    }
    return Future.succeededFuture();
}

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

License:Open Source License

Future<Void> loadTenantData() {

    if (getConfig().getFilename() == null) {
        return Future.succeededFuture();
    } else {//  w  w w  .ja va  2 s .  c  o m
        final Future<Buffer> readResult = Future.future();
        vertx.fileSystem().readFile(getConfig().getFilename(), readResult.completer());
        return readResult.compose(buffer -> {
            return addAll(buffer);
        }).recover(t -> {
            log.debug("cannot load tenants from file [{}]: {}", getConfig().getFilename(), t.getMessage());
            return Future.succeededFuture();
        });
    }
}

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

License:Open Source License

Future<Void> saveToFile() {

    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {

            final JsonArray tenantsJson = new JsonArray();
            tenants.values().stream().forEach(tenant -> {
                tenantsJson.add(JsonObject.mapFrom(tenant));
            });//from w  w  w. j  a  v  a  2s  . co m

            final Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(),
                    Buffer.factory.buffer(tenantsJson.encodePrettily()), writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} tenants to file {}", tenantsJson.size(),
                        getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write tenants to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("tenants registry does not need to be persisted");
        return Future.succeededFuture();
    }
}

From source file:org.eclipse.hono.messaging.ForwardingDownstreamAdapter.java

License:Open Source License

@Override
public final void onClientAttach(final UpstreamReceiver client,
        final Handler<AsyncResult<Void>> resultHandler) {

    if (!running) {
        throw new IllegalStateException("adapter must be started first");
    }/*from w w  w. j  a  va  2s .c  o m*/

    Objects.requireNonNull(client);
    Objects.requireNonNull(resultHandler);

    ProtonSender sender = activeSenders.get(client);
    if (sender != null && sender.isOpen()) {
        logger.info("reusing existing downstream sender [con: {}, link: {}]", client.getConnectionId(),
                client.getLinkId());
        resultHandler.handle(Future.succeededFuture());
    } else {
        removeSender(client);
        // register the result handler to be failed if the connection to the downstream container fails during
        // the attempt to create a downstream sender
        clientAttachHandlers.add(resultHandler);
        Future<Void> tracker = Future.future();
        tracker.setHandler(attempt -> {
            if (attempt.succeeded()) {
                logger.info("created downstream sender [con: {}, link: {}]", client.getConnectionId(),
                        client.getLinkId());
            } else {
                logger.warn("can't create downstream sender [con: {}, link: {}]: {}", client.getConnectionId(),
                        client.getLinkId(), attempt.cause().getMessage());
            }
            clientAttachHandlers.remove(resultHandler);
            resultHandler.handle(attempt);
        });

        final ResourceIdentifier targetAddress = ResourceIdentifier.fromString(client.getTargetAddress());
        createSender(targetAddress, replenishedSender -> handleFlow(replenishedSender, client))
                .compose(createdSender -> {
                    addSender(client, createdSender);
                    tracker.complete();
                }, tracker);
    }
}