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:com.github.ithildir.airbot.service.UserServiceVertxEBProxy.java

License:Apache License

public void getUserLocation(String userId, Handler<AsyncResult<Location>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;// w  w w.j a  v a  2 s  .  c  om
    }
    JsonObject _json = new JsonObject();
    _json.put("userId", userId);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "getUserLocation");
    _vertx.eventBus().<JsonObject>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future
                    .succeededFuture(res.result().body() == null ? null : new Location(res.result().body())));
        }
    });
}

From source file:com.github.ithildir.airbot.service.UserServiceVertxEBProxy.java

License:Apache License

public void updateUserLocation(String userId, double latitude, double longitude, String country,
        Handler<AsyncResult<Void>> handler) {
    if (closed) {
        handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from  w  w  w .  j  a  v  a 2 s.  co m*/
    }
    JsonObject _json = new JsonObject();
    _json.put("userId", userId);
    _json.put("latitude", latitude);
    _json.put("longitude", longitude);
    _json.put("country", country);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "updateUserLocation");
    _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            handler.handle(Future.failedFuture(res.cause()));
        } else {
            handler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void getAllIdsBlocking(AsyncResultHandler<List<String>> resultHandler) {
    redissonOther.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).keySetAsync()
            .addListener(future -> {/*w  ww  . ja v a2  s.c om*/
                resultHandler.handle(future.isSuccess()
                        ? (future.get() != null
                                ? Future.succeededFuture(
                                        ((Set<String>) future.get()).stream().collect(Collectors.toList()))
                                : Future.failedFuture(new RepositoryException("No search result returned")))
                        : Future.failedFuture(new RepositoryException(future.cause())));
                if (future.isSuccess()) {
                    logger.debug("[" + getStorageKey() + "] Get ALL Keys in Unique [" + getUniqueKey("id")
                            + "] Success.");
                } else {
                    logger.debug("[" + getStorageKey() + "] Get ALL Keys in Unique [" + getUniqueKey("id")
                            + "] Failed: ", future.cause());
                }
            });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void getByListBlocking(List<String> ids, AsyncResultHandler<List<T>> resultHandler) {
    if (ids == null) {
        resultHandler.handle(Future.failedFuture(new IllegalArgumentException("List of ids can't be null.")));
        return;//from   w w  w.  j a v  a  2  s  .  c o  m
    } else if (ids.isEmpty()) {
        resultHandler.handle(Future.succeededFuture(Collections.emptyList()));
        return;
    }
    AtomicLong c = new AtomicLong(0);
    ArrayList<T> l = new ArrayList<>(ids.size());
    IntStream.range(0, ids.size()).forEach(i -> l.add(null));
    ids.stream().forEach(e -> {
        getBlocking(e, r -> {
            l.set(ids.indexOf(e), r.result());
            if (c.incrementAndGet() == ids.size()) {
                resultHandler.handle(Future.succeededFuture(
                        l.stream().filter(s -> s != null).collect(Collectors.toCollection(ArrayList::new))));
            }
        });
    });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void searchIndexByPositionBlocking(String fieldName, String indexName, Integer start, Integer stop,
        AsyncResultHandler<List<String>> resultHandler) {
    redissonOther.getScoredSortedSet(getScoredSortedIndexKey(fieldName, indexName), StringCodec.INSTANCE)
            .valueRangeAsync(start, stop).addListener(future -> {
                Collection<String> res = (Collection<String>) future.get();
                try {
                    ArrayList<String> ids = res.stream().map(r -> lookupIdFromIndex(r, fieldName, indexName))
                            .collect(Collectors.toCollection(ArrayList::new));
                    resultHandler/*from  w w  w . j  av a 2 s.  c  o  m*/
                            .handle(future.isSuccess()
                                    ? (future.get() != null ? Future.succeededFuture(ids)
                                            : Future.failedFuture(
                                                    new RepositoryException("No search result returned")))
                                    : Future.failedFuture(new RepositoryException(future.cause())));
                    if (future.isSuccess()) {
                        logger.debug("[" + getStorageKey() + "] Search Index By Position Success. Records: "
                                + ((Collection<String>) future.get()).size());
                    } else {
                        logger.debug("[" + getStorageKey() + "] Search Index By Position Failure.",
                                future.cause());
                    }
                } catch (RuntimeException e) {
                    resultHandler.handle(Future.failedFuture(e.getCause()));
                    logger.debug("[" + getStorageKey() + "] Search Index By Position Failure.", e);
                }
            });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void searchIndexByScoreBlocking(String fieldName, String indexName, Double min, Double max,
        Integer offset, Integer limit, AsyncResultHandler<List<String>> resultHandler) {
    redissonOther//  w  ww  .  jav  a 2 s  .  c om
            .<String>getScoredSortedSet(getScoredSortedIndexKey(fieldName, indexName), StringCodec.INSTANCE)
            .valueRangeAsync(min, true, max, true, offset, limit).addListener(future -> {
                Collection<String> res = (Collection<String>) future.get();
                try {
                    ArrayList<String> ids = res.stream().map(r -> lookupIdFromIndex(r, fieldName, indexName))
                            .collect(Collectors.toCollection(ArrayList::new));
                    resultHandler
                            .handle(future.isSuccess()
                                    ? (future.get() != null || ids == null ? Future.succeededFuture(ids)
                                            : Future.failedFuture(
                                                    new RepositoryException("No search result returned")))
                                    : Future.failedFuture(new RepositoryException(future.cause())));
                    if (future.isSuccess()) {
                        logger.debug("[" + getStorageKey() + "] Search Index By Score Success. Records: "
                                + ((Collection<String>) future.get()).size());
                    } else {
                        logger.debug("[" + getStorageKey() + "] Search Index By Score Failure.",
                                future.cause());
                    }
                } catch (RuntimeException e) {
                    resultHandler.handle(Future.failedFuture(e.getCause()));
                    logger.debug("[" + getStorageKey() + "] Search Index By Score Failure.", e);
                }
            });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void searchIndexByValueBlocking(String fieldName, String indexName, String min, String max,
        Integer offset, Integer limit, AsyncResultHandler<List<String>> resultHandler) {
    redissonOther.<String>getLexSortedSet(getScoredSortedIndexKey(fieldName, indexName))
            .lexRangeAsync(min, true, max, true, offset, limit).addListener(future -> {
                Collection<String> res = (Collection<String>) future.get();
                try {
                    ArrayList<String> ids = res.stream().map(r -> lookupIdFromIndex(r, fieldName, indexName))
                            .collect(Collectors.toCollection(ArrayList::new));
                    resultHandler// ww w.j  a va 2 s . c  o  m
                            .handle(future.isSuccess()
                                    ? (future.get() != null ? Future.succeededFuture(ids)
                                            : Future.failedFuture(
                                                    new RepositoryException("No search result returned")))
                                    : Future.failedFuture(new RepositoryException(future.cause())));
                    if (future.isSuccess()) {
                        logger.debug("[" + getStorageKey() + "] Search Index By Value Success. Records: "
                                + ((Collection<String>) future.get()).size());
                    } else {
                        logger.debug("[" + getStorageKey() + "] Search Index By Value Failure.",
                                future.cause());
                    }
                } catch (RuntimeException e) {
                    resultHandler.handle(Future.failedFuture(e.getCause()));
                    logger.debug("[" + getStorageKey() + "] Search Index By Value Failure.", e);
                }
            });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void deleteBlocking(String id, Handler<AsyncResult<Boolean>> resultHandler) {
    RBatch batch = redissonWrite.createBatch();
    //remove the indexes.
    batch.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).fastRemoveAsync(id);
    batch.getAtomicLongAsync(getCounterKey()).decrementAndGetAsync();
    batch.executeAsync().addListener(future -> {
        resultHandler.handle(future.isSuccess()
                ? (future.get() != null ? Future.succeededFuture(((Integer) ((List) future.get()).get(0)) == 1)
                        : Future.failedFuture(new RepositoryException("No batch result returned")))
                : Future.failedFuture(new RepositoryException(future.cause())));
        if (future.isSuccess()) {
            logger.debug("[" + getStorageKey() + "] Delete Success : " + id);
        } else {//from  ww  w  . j  a  v  a 2s .  co m
            logger.debug("[" + getStorageKey() + "] Delete Failure : " + id, future.cause());
        }
    });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

@Override
public void totalCount(Handler<AsyncResult<Long>> resultHandler) {
    redissonOther.<String, String>getAtomicLong(getCounterKey()).getAsync().addListener(future -> {
        resultHandler.handle(future.isSuccess()
                ? (future.get() != null ? Future.succeededFuture((Long) future.get())
                        : Future.failedFuture(new RepositoryException("No search result returned")))
                : Future.failedFuture(new RepositoryException(future.cause())));
        if (future.isSuccess()) {
            logger.debug("[" + getCounterKey() + "] Count Success : " + (Long) future.get());
        } else {/*w  ww  .  ja v  a  2 s  .  com*/
            logger.debug("[" + getCounterKey() + "] Count Failure ", future.cause());
        }
    });
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void idExist(String id, Handler<AsyncResult<Boolean>> resultHandler) {
    redissonOther.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).containsKeyAsync(id)
            .addListener(future -> {/*from ww w . jav a2  s.  co m*/
                resultHandler
                        .handle(future.isSuccess()
                                ? (future.get() != null ? Future.succeededFuture((Boolean) future.get())
                                        : Future.failedFuture(
                                                new RepositoryException("No search result returned")))
                                : Future.failedFuture(new RepositoryException(future.cause())));
                if (future.isSuccess()) {
                    logger.debug("[" + getStorageKey() + "] ID Exist Success : " + id);
                } else {
                    logger.debug("[" + getStorageKey() + "] ID Exist Failure : " + id, future.cause());
                }
            });
}