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.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void getIdByUnique(String fieldName, String value, AsyncResultHandler<String> resultHandler) {
    redissonOther.<String, String>getMap(getUniqueKey(fieldName)).getAsync(value).addListener(future -> {
        resultHandler.handle(future.isSuccess()
                ? (future.get() != null ? Future.succeededFuture((String) future.get())
                        : Future.failedFuture(new RepositoryException("No search result returned")))
                : Future.failedFuture(new RepositoryException(future.cause())));
        if (future.isSuccess()) {
            logger.debug("[" + getStorageKey() + "] Get ID By Unique Success : " + value + " - "
                    + (String) future.get());
        } else {//from w  ww .  j  av  a  2 s  .  c  o  m
            logger.debug("[" + getStorageKey() + "] Get ID By Unique : " + value, future.cause());
        }
    });
}

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

License:Apache License

private void indexValue(String id, String fieldName, RedissonIndex index, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {//from   ww  w  .  j av  a2s .  co  m
        Object value = instance.getValue(fieldName);
        String resolve = index.valueResolver().newInstance().resolve(value, instance, id, fieldName, index);
        GenericFutureListener<io.netty.util.concurrent.Future<Boolean>> futureListener = (
                io.netty.util.concurrent.Future<Boolean> future) -> {
            resultHandler
                    .handle(future.isSuccess() && future.get() != null ? Future.succeededFuture(future.get())
                            : Future.failedFuture(future.cause()));
        };
        if (index.scoreResolver().isAssignableFrom(LexicalScoreResolver.class)) {
            redissonOther.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name())).addAsync(resolve)
                    .addListener(futureListener);
        } else {
            Double score = index.scoreResolver().newInstance().resolve(value, instance, id, fieldName, index);
            redissonOther
                    .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()), StringCodec.INSTANCE)
                    .addAsync(score, resolve).addListener(futureListener);
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

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

License:Apache License

private void unindexValue(String id, String fieldName, RedissonIndex index, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {//from  ww w .j av  a  2s .  com
        Object value = instance.getValue(fieldName);
        String resolve = index.valueResolver().newInstance().resolve(value, instance, id, fieldName, index);
        GenericFutureListener<io.netty.util.concurrent.Future<Boolean>> futureListener = (
                io.netty.util.concurrent.Future<Boolean> future) -> {
            resultHandler
                    .handle(future.isSuccess() && future.get() != null ? Future.succeededFuture(future.get())
                            : Future.failedFuture(future.cause()));
        };
        if (index.scoreResolver().isAssignableFrom(LexicalScoreResolver.class)) {
            redissonOther.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name())).removeAsync(resolve)
                    .addListener(futureListener);
        } else {
            redissonOther
                    .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()), StringCodec.INSTANCE)
                    .removeAsync(resolve).addListener(futureListener);
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

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

License:Apache License

private void newId(AsyncResultHandler<String> resultHandler) {
    redissonOther.getAtomicLong(getSequenceKey()).incrementAndGetAsync().addListener(future -> {
        resultHandler.handle(future.isSuccess()
                ? (future.get() != null ? Future.succeededFuture(future.get().toString())
                        : Future.failedFuture(new RepositoryException("No sequence returned")))
                : Future.failedFuture(new RepositoryException(future.cause())));
        if (future.isSuccess()) {
            logger.debug("[" + getStorageKey() + "] New ID Success : " + future.get().toString());
        } else {/*from  w  ww . ja  v  a  2 s .  c om*/
            logger.debug("[" + getStorageKey() + "] New ID Failure", future.cause());
        }
    });
}

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

License:Apache License

private void persistBlocking(String id, JsonObject data, RBatch redissonBatch,
        Handler<AsyncResult<Boolean>> resultHandler) {
    RBatch batch = redissonBatch == null ? redissonWrite.createBatch() : redissonBatch;
    AtomicBoolean failed = new AtomicBoolean(false);
    try {/*from ww w. j  ava  2  s  .c o  m*/
        BeanMap pMap = new BeanMap(cls.newInstance());
        //remove the indexes;
        if (isRedisEntity()) {
            AtomicBoolean finished = new AtomicBoolean(false);
            AtomicBoolean hasNested = new AtomicBoolean(false);
            AtomicLong stack = new AtomicLong();
            pMap.forEach((k, v) -> {
                if ("class".equals(k)) {
                    return;
                }
                Class<?> type = pMap.getType((String) k);
                if (!isRedisEntity(type)) {
                    //recreate the indexes;
                    if ("id".equals(k)) {
                        batch.getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id, id);
                    } else {
                        batch.getMap(getStorageKey((String) k)).fastPutAsync(id, data.getValue((String) k));
                    }
                } else {
                    hasNested.set(true);
                    stack.incrementAndGet();
                    RedisRepositoryImpl<?> innerRepo;
                    try {
                        innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    } catch (RepositoryException e) {
                        throw new RuntimeException(e);
                    }
                    JsonObject value = data.getJsonObject((String) k);
                    final boolean newOne = !value.containsKey("id") || value.getString("id") == null
                            || "null".equals(value.getString("id"));
                    final String ID = newOne ? id : value.getString("id");
                    innerRepo.persist(ID, value, batch, c -> {//making the nested entity shares the same id as the parent when its 1:1 relation. This makes fetch a lot faster since it doesn't not need to resolve the reference when fetching 1:1 nested objects.
                        if (c.succeeded()) {
                            long s = stack.decrementAndGet();
                            if (newOne) {
                                batch.getMap(getStorageKey((String) k)).fastPutAsync(id, ID);//different to the update, create needs to add the reference field to batch
                            }
                            if (s == 0 && finished.get() && !failed.get()) { //finished iterating and no outstanding processes. 
                                if (redissonBatch == null) {//if it's not inside a nested process.
                                    finishPersist(id, data, batch, resultHandler);
                                } else {//if it is inside a nested process.
                                    resultHandler.handle(Future.succeededFuture(true));
                                }
                            }
                            //else wait for others to complete
                        } else {
                            boolean firstToFail = failed.compareAndSet(false, true);
                            if (firstToFail) {
                                resultHandler.handle(Future.failedFuture(c.cause()));
                            }
                        }
                    });
                }
            });
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            finished.set(true);
            if (!hasNested.get()) {//does not have nested RedissonEntity within
                if (redissonBatch == null) {//if it's not inside a nested process.
                    finishPersist(id, data, batch, resultHandler);
                } else {//if it is inside a nested process.
                    resultHandler.handle(Future.succeededFuture(true));
                }
            }
        } else {//not a RedissonEntity class, persist as json string.
            //recreate the indexes;
            batch.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id,
                    Json.encode(data));
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            if (redissonBatch == null) {//if it's not inside a nested process.
                finishPersist(id, data, batch, resultHandler);
            } else {//if it is inside a nested process.
                resultHandler.handle(Future.succeededFuture(true));
            }
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        failed.set(true);
        resultHandler.handle(Future.failedFuture(ex));
    }
}

From source file:com.github.meshuga.vertx.neo4j.rbac.auth.AuthServiceVertxEBProxy.java

License:Apache License

public void hasPermission(String userName, String permission, Handler<AsyncResult<Boolean>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*ww w  . j  ava2 s .c  om*/
    }
    JsonObject _json = new JsonObject();
    _json.put("userName", userName);
    _json.put("permission", permission);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "hasPermission");
    _vertx.eventBus().<Boolean>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:com.glt.cronjob.JobServiceVertxEBProxy.java

License:Apache License

public JobService schedule(JsonObject jobDescriptor, Handler<AsyncResult<Boolean>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from  w  w w. ja  v  a 2s .  c  o  m*/
    JsonObject _json = new JsonObject();
    _json.put("jobDescriptor", jobDescriptor);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "schedule");
    _vertx.eventBus().<Boolean>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:com.glt.cronjob.JobServiceVertxEBProxy.java

License:Apache License

public JobService unschedule(JsonObject jobDescriptor, Handler<AsyncResult<Boolean>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from www.j a  va 2  s.c om
    JsonObject _json = new JsonObject();
    _json.put("jobDescriptor", jobDescriptor);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "unschedule");
    _vertx.eventBus().<Boolean>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
    return this;
}

From source file:com.glt.rest.client.RestServiceVertxEBProxy.java

License:Apache License

public void get(JsonObject command, Handler<AsyncResult<String>> asyncHandler) {
    if (closed) {
        asyncHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from w  w w  .  java  2 s.c o  m
    }
    JsonObject _json = new JsonObject();
    _json.put("command", command);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "get");
    _vertx.eventBus().<String>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            asyncHandler.handle(Future.failedFuture(res.cause()));
        } else {
            asyncHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}

From source file:com.glt.rest.client.RestServiceVertxEBProxy.java

License:Apache License

public void post(JsonObject command, Handler<AsyncResult<String>> asyncHandler) {
    if (closed) {
        asyncHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from w  ww  .  ja v a 2 s  . c  o  m*/
    }
    JsonObject _json = new JsonObject();
    _json.put("command", command);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "post");
    _vertx.eventBus().<String>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            asyncHandler.handle(Future.failedFuture(res.cause()));
        } else {
            asyncHandler.handle(Future.succeededFuture(res.result().body()));
        }
    });
}