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:com.github.ithildir.airbot.util.AirQualityMessageBuilder.java

License:Open Source License

public Future<String> getMessage(Location location, String locationString, Locale locale) {

    Future<String> future = Future.future();

    MeasurementService measurementService = _getMeasurementService(location);

    Future<Measurement> measurementFuture = Future.future();
    Future<String> nameFuture = Future.future();

    measurementService.getMeasurement(location.getLatitude(), location.getLongitude(), measurementFuture);

    measurementService.getName(nameFuture);

    CompositeFuture compositeFuture = CompositeFuture.all(measurementFuture, nameFuture);

    compositeFuture.setHandler(asyncResult -> {
        if (asyncResult.failed()) {
            future.fail(asyncResult.cause());

            return;
        }/*www .  j  a v a 2  s.c  o m*/

        CompositeFuture resultCompositeFuture = asyncResult.result();

        Measurement measurement = (Measurement) resultCompositeFuture.resultAt(0);
        String name = (String) resultCompositeFuture.resultAt(1);

        String message = _getMessage(measurement, name, locationString, locale);

        future.complete(message);
    });

    return future;
}

From source file:com.github.mcollovati.vertx.vaadin.VertxVaadinRequest.java

License:Open Source License

@Override
public boolean isUserInRole(String role) {
    if (routingContext.user() != null) {
        Future<Boolean> userInRole = Future.future();
        return Sync.await(completer -> routingContext.user().isAuthorised(role, completer));
    }/* ww  w  .j  av  a  2  s. c o m*/
    return false;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

public Future<RetrieveCommandResponse> get(Collection<String> keys) {
    Future<RetrieveCommandResponse> finalResult = Future.future();

    MemcacheClientMultiResponseHandler handleWrapper = new MemcacheClientMultiResponseHandler(finalResult,
            keys.size());//w  ww . j av a 2s. co  m
    for (String key : keys) {
        retrieve(MemcacheCommandType.get, key).setHandler(handleWrapper);
    }

    return finalResult;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

public Future<DeleteCommandResponse> delete(String key) {
    Future<DeleteCommandResponse> finalResult = Future.future();

    MemcacheCommand command = new MemcacheCommand(MemcacheCommandType.delete, getCacheKey(key), null, null);

    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(INFINITE_REPLY_TIMEOUT);
    eventBus.send(getEventBusAddress(key), command, deliveryOptions,
            new MemcacheClientResponseHandler<>(finalResult));

    return finalResult;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

public Future<TouchCommandResponse> touch(String key, int expires) {
    Future<TouchCommandResponse> finalResult = Future.future();

    MemcacheCommand command = new MemcacheCommand(MemcacheCommandType.touch, getCacheKey(key), null, expires);

    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(INFINITE_REPLY_TIMEOUT);
    eventBus.send(getEventBusAddress(key), command, deliveryOptions,
            new MemcacheClientResponseHandler<>(finalResult));

    return finalResult;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

private Future<RetrieveCommandResponse> retrieve(MemcacheCommandType commandType, String key) {
    Future<RetrieveCommandResponse> finalResult = Future.future();

    MemcacheCommand command = new MemcacheCommand(commandType, getCacheKey(key), null, null);

    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(INFINITE_REPLY_TIMEOUT);
    eventBus.send(getEventBusAddress(key), command, deliveryOptions,
            new TranslateKeyResponseHandler(finalResult, key, command.getKey()));

    return finalResult;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

private Future<ModifyCommandResponse> modify(MemcacheCommandType commandType, String key, String data) {
    Future<ModifyCommandResponse> finalResult = Future.future();

    MemcacheCommand command = new MemcacheCommand(commandType, getCacheKey(key), data, null);

    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(INFINITE_REPLY_TIMEOUT);
    eventBus.send(getEventBusAddress(key), command, deliveryOptions,
            new MemcacheClientResponseHandler<>(finalResult));

    return finalResult;
}

From source file:com.groupon.vertx.memcache.client.MemcacheClient.java

License:Apache License

private Future<StoreCommandResponse> store(MemcacheCommandType commandType, String key, String data,
        int expires) {
    Future<StoreCommandResponse> finalResult = Future.future();

    MemcacheCommand command = new MemcacheCommand(commandType, getCacheKey(key), data, expires);

    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(INFINITE_REPLY_TIMEOUT);
    eventBus.send(getEventBusAddress(key), command, deliveryOptions,
            new MemcacheClientResponseHandler<>(finalResult));

    return finalResult;
}

From source file:com.groupon.vertx.redis.RedisClient.java

License:Apache License

@Override
protected Future<JsonObject> sendCommand(RedisCommand command) {
    final Future<JsonObject> finalResult = Future.future();
    final DeliveryOptions deliveryOptions = new DeliveryOptions().setSendTimeout(replyTimeout);
    eventBus.send(eventBusAddress, new JsonObject().put("commands", new JsonArray().add(command.toJson())),
            deliveryOptions, new Handler<AsyncResult<Message<JsonObject>>>() {
                @Override/*from  w  w w .  j av a2  s  .com*/
                public void handle(AsyncResult<Message<JsonObject>> messageAsyncResult) {
                    if (messageAsyncResult.succeeded() && messageAsyncResult.result() != null) {
                        finalResult.complete(messageAsyncResult.result().body());
                    } else {
                        RedisCommandException exception;
                        if (messageAsyncResult.cause() != null) {
                            String errorMessage;
                            final Throwable cause = messageAsyncResult.cause();
                            if (cause instanceof ReplyException) {
                                errorMessage = createErrorJson(((ReplyException) cause).failureType().name());
                            } else {
                                errorMessage = createErrorJson(cause.getMessage());
                            }
                            exception = new RedisCommandException(errorMessage);
                            exception.addSuppressed(messageAsyncResult.cause());
                        } else {
                            exception = new RedisCommandException(
                                    createErrorJson(HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase()));
                        }
                        finalResult.fail(exception);
                    }
                }
            });
    return finalResult;
}

From source file:com.groupon.vertx.redis.RedisCommandHandler.java

License:Apache License

private void setCommandResponseHandler(final List<RedisCommand> redisCommands,
        final Message<JsonObject> command, final boolean isMulti) {
    for (final RedisCommand redisCommand : redisCommands) {
        final Future<JsonObject> finalResult = Future.future();
        finalResult.setHandler(new Handler<AsyncResult<JsonObject>>() {
            public void handle(AsyncResult<JsonObject> commandResponse) {
                log.trace("handleCommand", "reply", new String[] { "command", "response", "isMulti" },
                        redisCommand.toString(), commandResponse, isMulti);
                if (commandResponse.succeeded()) {
                    command.reply(commandResponse.result());
                } else {
                    String cause = commandResponse.cause() != null ? commandResponse.cause().getMessage()
                            : "unknown";
                    command.reply(buildReply("error", null, cause));
                }/* w w  w  .j a  v a2s .  c om*/
            }
        });
        redisCommand.commandResponse(finalResult);
    }
}