Example usage for io.vertx.core Future isComplete

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

Introduction

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

Prototype

boolean isComplete();

Source Link

Document

Has the future completed?

Usage

From source file:com.consol.citrus.vertx.factory.AbstractVertxInstanceFactory.java

License:Apache License

/**
 * Creates new Vert.x instance with default factory. Subclasses may overwrite this
 * method in order to provide special Vert.x instance.
 * @return/*from w  w w.  jav a2 s . c  om*/
 */
protected Vertx createVertx(VertxEndpointConfiguration endpointConfiguration) {
    final Vertx[] vertx = new Vertx[1];
    final Future loading = new FutureFactoryImpl().future();

    Handler<AsyncResult<Vertx>> asyncLoadingHandler = new Handler<AsyncResult<Vertx>>() {
        @Override
        public void handle(AsyncResult<Vertx> event) {
            vertx[0] = event.result();
            loading.complete();
            log.info("Vert.x instance started");
        }
    };

    if (endpointConfiguration.getPort() > 0) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Creating new Vert.x instance '%s:%s' ...", endpointConfiguration.getHost(),
                    endpointConfiguration.getPort()));
        }
        VertxOptions vertxOptions = new VertxOptions();
        vertxOptions.setClusterPort(endpointConfiguration.getPort());
        vertxOptions.setClusterHost(endpointConfiguration.getHost());
        vertxFactory.clusteredVertx(vertxOptions, asyncLoadingHandler);
    } else {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Creating new Vert.x instance '%s:%s' ...", endpointConfiguration.getHost(),
                    0L));
        }
        VertxOptions vertxOptions = new VertxOptions();
        vertxOptions.setClusterPort(0);
        vertxOptions.setClusterHost(endpointConfiguration.getHost());
        vertxFactory.clusteredVertx(vertxOptions, asyncLoadingHandler);
    }

    // Wait for full loading
    while (!loading.isComplete()) {
        try {
            log.debug("Waiting for Vert.x instance to startup");
            Thread.sleep(250L);
        } catch (InterruptedException e) {
            log.warn("Interrupted while waiting for Vert.x instance startup", e);
        }
    }

    return vertx[0];
}

From source file:io.nonobot.core.client.impl.BotClientImpl.java

License:Apache License

@Override
public void receiveMessage(ReceiveOptions options, String message, Handler<AsyncResult<String>> replyHandler) {
    String replyAddress = UUID.randomUUID().toString();
    Future<String> reply = Future.future();
    reply.setHandler(replyHandler);/* ww  w  . j  av a  2  s.c o  m*/
    MessageConsumer<String> consumer = vertx.eventBus().consumer(replyAddress);
    consumer.handler(msg -> {
        String content = msg.body();
        if (content != null && !reply.isComplete()) {
            if (msg.replyAddress() != null) {
                msg.reply(null);
            }
            reply.complete(content);
            consumer.unregister();
        } else {
            if (msg.replyAddress() != null) {
                msg.fail(0, "Already replied");
            }
        }
    });
    consumer.completionHandler(ar -> {
        if (ar.succeeded()) {
            Matcher botMatcher = botPattern.matcher(message);
            JsonObject msg = new JsonObject().put("replyAddress", replyAddress);
            msg.put("chatId", options.getChatId());
            if (botMatcher.find()) {
                msg.put("respond", true);
                msg.put("content", botMatcher.group(1));
            } else {
                msg.put("respond", false);
                msg.put("content", message);
            }
            vertx.eventBus().publish(inboundAddress, msg);
            vertx.setTimer(options.getTimeout(), timerID -> {
                if (!reply.isComplete()) {
                    consumer.unregister();
                    reply.fail(new Exception("timeout"));
                }
            });
        } else {
            replyHandler.handle(Future.failedFuture(ar.cause()));
        }
    });
}

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

License:Open Source License

/**
 * Verifies username/password credentials with a remote authentication server using SASL PLAIN.
 * /*from  www.  j  a v  a  2  s .  co  m*/
 * @param authzid The identity to act as.
 * @param authcid The username.
 * @param password The password.
 * @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication,
 *                                    the result contains a JWT with the authenticated user's claims.
 */
public void verifyPlain(final String authzid, final String authcid, final String password,
        final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {

    final ProtonClientOptions options = new ProtonClientOptions();
    options.setReconnectAttempts(3).setReconnectInterval(50);
    options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN);
    factory.connect(options, authcid, password, null, null, conAttempt -> {
        if (conAttempt.failed()) {
            authenticationResultHandler.handle(Future.failedFuture("cannot connect to Authentication service"));
        } else {
            final ProtonConnection openCon = conAttempt.result();

            final Future<HonoUser> userTracker = Future.future();
            userTracker.setHandler(s -> {
                if (s.succeeded()) {
                    authenticationResultHandler.handle(Future.succeededFuture(s.result()));
                } else {
                    authenticationResultHandler.handle(Future.failedFuture(s.cause()));
                }
                final ProtonConnection con = conAttempt.result();
                if (con != null) {
                    LOG.debug("closing connection to Authentication service");
                    con.close();
                }
            });

            vertx.setTimer(5000, tid -> {
                if (!userTracker.isComplete()) {
                    userTracker.fail("time out reached while waiting for token from Authentication service");
                }
            });

            getToken(openCon, userTracker);
        }
    });
}

From source file:org.eclipse.hono.service.auth.delegating.AuthenticationServerClient.java

License:Open Source License

/**
 * Verifies username/password credentials with a remote authentication server using SASL PLAIN.
 * /* w  ww  .  j  a  va 2  s  .co m*/
 * @param authzid The identity to act as.
 * @param authcid The username.
 * @param password The password.
 * @param authenticationResultHandler The handler to invoke with the authentication result. On successful authentication,
 *                                    the result contains a JWT with the the authenticated user's claims.
 */
public void verifyPlain(final String authzid, final String authcid, final String password,
        final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {

    ProtonClientOptions options = new ProtonClientOptions();
    options.setReconnectAttempts(3).setReconnectInterval(50);
    options.addEnabledSaslMechanism(AuthenticationConstants.MECHANISM_PLAIN);
    factory.connect(options, authcid, password, null, null, conAttempt -> {
        if (conAttempt.failed()) {
            authenticationResultHandler.handle(Future.failedFuture("cannot connect to authentication server"));
        } else {
            final ProtonConnection openCon = conAttempt.result();

            final Future<HonoUser> userTracker = Future.future();
            userTracker.setHandler(s -> {
                if (s.succeeded()) {
                    authenticationResultHandler.handle(Future.succeededFuture(s.result()));
                } else {
                    authenticationResultHandler.handle(Future.failedFuture(s.cause()));
                }
                ProtonConnection con = conAttempt.result();
                if (con != null) {
                    LOG.debug("closing connection to authentication server");
                    con.close();
                }
            });

            vertx.setTimer(5000, tid -> {
                if (!userTracker.isComplete()) {
                    userTracker.fail("time out reached while waiting for token from authentication server");
                }
            });

            getToken(openCon, userTracker);
        }
    });
}