Example usage for io.vertx.core Future failedFuture

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

Introduction

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

Prototype

static <T> Future<T> failedFuture(String failureMessage) 

Source Link

Document

Create a failed future with the specified failure message.

Usage

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 {//  w  w w .  j  av a  2 s  .  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());
        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 {// ww  w.  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.messaging.ForwardingDownstreamAdapter.java

License:Open Source License

private void connectToDownstream(final ProtonClientOptions options,
        final Handler<AsyncResult<ProtonConnection>> connectResultHandler) {

    downstreamConnectionFactory.connect(options, this::onRemoteClose, this::onDisconnectFromDownstreamContainer,
            connectAttempt -> {//from w  w w.ja v a 2s.  com
                if (connectAttempt.succeeded()) {
                    this.downstreamConnection = connectAttempt.result();
                    metrics.incrementDownStreamConnections();
                    if (connectResultHandler != null) {
                        connectResultHandler.handle(Future.succeededFuture(connectAttempt.result()));
                    }
                } else {
                    logger.info("failed to connect to downstream container: {}",
                            connectAttempt.cause().getMessage());
                    if (retryOnFailedConnectAttempt) {
                        reconnect(connectResultHandler);
                    } else if (connectResultHandler != null) {
                        connectResultHandler.handle(Future.failedFuture(connectAttempt.cause()));
                    }
                }
            });
}

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

License:Open Source License

/**
 * Handles unexpected disconnection from downstream container.
 * <p>//  w ww  .j a v  a 2 s  .c  o  m
 * Clears all internal state kept for the connection, e.g. open links etc, and then tries to
 * reconnect.
 * 
 * @param con The failed connection.
 */
private void onDisconnectFromDownstreamContainer(final ProtonConnection con) {

    if (con != downstreamConnection) {
        logger.warn("unknown connection to downstream container has been disconnected");
    } else {
        // all links to downstream host will now be stale and unusable
        logger.warn("lost connection to downstream container [{}], closing upstream receivers ...",
                con.getRemoteContainer());

        for (UpstreamReceiver client : activeSenders.keySet()) {
            client.close(ErrorConditions.ERROR_NO_DOWNSTREAM_CONSUMER);
            metrics.decrementUpstreamLinks(client.getTargetAddress());
            metrics.decrementDownstreamSenders(client.getTargetAddress());
            metrics.submitDownstreamLinkCredits(client.getTargetAddress(), 0);
        }
        receiversPerConnection.clear();
        activeSenders.clear();
        downstreamConnection.attachments().clear();
        downstreamConnection.disconnectHandler(null);
        downstreamConnection.disconnect();
        metrics.decrementDownStreamConnections();

        for (Iterator<Handler<AsyncResult<Void>>> iter = clientAttachHandlers.iterator(); iter.hasNext();) {
            iter.next().handle(Future.failedFuture("connection to downstream container failed"));
            iter.remove();
        }

        reconnect(null);
    }
}

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

License:Open Source License

private Future<ProtonSender> createSender(final ResourceIdentifier targetAddress,
        final Handler<ProtonSender> sendQueueDrainHandler) {

    if (!isConnected()) {
        return Future.failedFuture("downstream connection must be opened before creating sender");
    } else {// w ww .  j  a va  2 s.  c  o  m
        return senderFactory.createSender(downstreamConnection, targetAddress, getDownstreamQos(),
                sendQueueDrainHandler);
    }
}

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

License:Open Source License

@Override
public Future<ProtonSender> createSender(final ProtonConnection connection, final ResourceIdentifier address,
        final ProtonQoS qos, final Handler<ProtonSender> sendQueueDrainHandler) {

    Objects.requireNonNull(connection);
    Objects.requireNonNull(address);
    Objects.requireNonNull(qos);/*from www .j a  v  a2 s.com*/

    if (connection.isDisconnected()) {
        return Future.failedFuture("connection is disconnected");
    } else {
        return newSession(connection, address).compose(session -> {
            return newSender(connection, session, address, qos, sendQueueDrainHandler);
        });
    }
}

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

License:Open Source License

private void connectToDownstream(final ProtonClientOptions options,
        final Handler<AsyncResult<ProtonConnection>> connectResultHandler) {

    downstreamConnectionFactory.connect(options, this::onRemoteClose, this::onDisconnectFromDownstreamContainer,
            connectAttempt -> {/*from w w  w .  ja v  a 2 s.  c o  m*/
                if (connectAttempt.succeeded()) {
                    this.downstreamConnection = connectAttempt.result();
                    connectResultHandler.handle(Future.succeededFuture(connectAttempt.result()));
                    counterService.increment(MetricConstants.metricNameDownstreamConnections());
                } else {
                    logger.info("failed to connect to downstream container", connectAttempt.cause());
                    connectResultHandler.handle(Future.failedFuture(connectAttempt.cause()));
                }
            });
}

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

License:Open Source License

/**
 * Handles unexpected disconnection from downstream container.
 * //from   w  w w  .ja v a 2s. c om
 * @param con The failed connection.
 */
private void onDisconnectFromDownstreamContainer(final ProtonConnection con) {

    if (con != downstreamConnection) {
        logger.warn("unknown connection to downstream container has been disconnected");
    } else {
        // all links to downstream host will now be stale and unusable
        logger.warn("lost connection to downstream container [{}], closing upstream receivers ...",
                con.getRemoteContainer());

        for (UpstreamReceiver client : activeSenders.keySet()) {
            client.close(ErrorConditions.ERROR_NO_DOWNSTREAM_CONSUMER);
        }
        sendersPerConnection.clear();
        activeSenders.clear();
        downstreamConnection.disconnectHandler(null);
        downstreamConnection.disconnect();
        counterService.decrement(MetricConstants.metricNameDownstreamConnections());

        for (Iterator<Handler<AsyncResult<Void>>> iter = clientAttachHandlers.iterator(); iter.hasNext();) {
            iter.next().handle(Future.failedFuture("connection to downstream container failed"));
            iter.remove();
        }

        final ProtonClientOptions clientOptions = createClientOptions();
        if (clientOptions.getReconnectAttempts() != 0) {
            vertx.setTimer(RECONNECT_INTERVAL_MILLIS, reconnect -> {
                logger.info("attempting to re-connect to downstream container");
                connectToDownstream(clientOptions);
            });
        }
    }
}

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

License:Open Source License

private Future<ProtonSender> createSender(final String targetAddress,
        final Handler<ProtonSender> sendQueueDrainHandler) {

    if (!isConnected()) {
        return Future.failedFuture("downstream connection must be opened before creating sender");
    } else {/*w w  w  .j av a 2 s  . c  om*/
        String tenantOnlyTargetAddress = getTenantOnlyTargetAddress(targetAddress);
        String address = tenantOnlyTargetAddress.replace(Constants.DEFAULT_PATH_SEPARATOR,
                honoConfig.getPathSeparator());
        return senderFactory.createSender(downstreamConnection, address, getDownstreamQos(),
                sendQueueDrainHandler);
    }
}

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

License:Open Source License

@Override
public Future<ProtonSender> createSender(final ProtonConnection connection, final String address,
        final ProtonQoS qos, final Handler<ProtonSender> sendQueueDrainHandler) {

    Objects.requireNonNull(connection);
    Objects.requireNonNull(address);
    Objects.requireNonNull(qos);//from  w  ww . j a  v  a 2s  .  c  o  m

    if (connection.isDisconnected()) {
        return Future.failedFuture("connection is disconnected");
    } else {
        return newSession(connection).compose(session -> {
            return newSender(connection, session, address, qos, sendQueueDrainHandler);
        });
    }
}