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

License:Apache License

private void indexValueBlocking(String id, String fieldName, String indexName, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {//from  w ww.ja  v  a 2  s.  c o m
        if (cls.getDeclaredField(fieldName).isAnnotationPresent(RedissonIndex.class)) {
            RedissonIndex index = cls.getDeclaredField(fieldName).getAnnotation(RedissonIndex.class);
            indexValue(id, fieldName, index, instance, resultHandler);
        } else if (cls.getDeclaredField(fieldName).isAnnotationPresent(RedissonIndex.List.class)) {
            RedissonIndex.List indexList = cls.getDeclaredField(fieldName)
                    .getAnnotation(RedissonIndex.List.class);
            Arrays.stream(indexList.value()).filter(e -> e.name().equals(indexName)).findFirst()
                    .ifPresent(index -> indexValue(id, fieldName, index, instance, resultHandler));
        }
    } catch (NoSuchFieldException | RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

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 w  ww. j  a  va 2s  .c  om*/
        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 unindexValueBlocking(String id, String fieldName, String indexName, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {/*from   w w  w .j a  v  a  2  s  .co m*/
        if (cls.getDeclaredField(fieldName).isAnnotationPresent(RedissonIndex.class)) {
            RedissonIndex index = cls.getDeclaredField(fieldName).getAnnotation(RedissonIndex.class);
            unindexValue(id, fieldName, index, instance, resultHandler);
        } else if (cls.getDeclaredField(fieldName).isAnnotationPresent(RedissonIndex.List.class)) {
            RedissonIndex.List indexList = cls.getDeclaredField(fieldName)
                    .getAnnotation(RedissonIndex.List.class);
            Arrays.stream(indexList.value()).filter(e -> e.name().equals(indexName)).findFirst()
                    .ifPresent(index -> unindexValue(id, fieldName, index, instance, resultHandler));
        }
    } catch (NoSuchFieldException | 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 w w  w . j  a va2s .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 reIndexBlocking(String id, T r, JsonObject data, Boolean isNew,
        Handler<AsyncResult<Void>> resultHandler) {

    RBatch updateIndexBatch = redissonOther.createBatch();
    ArrayList<String> pList = new ArrayList();
    try {/*from w  w w . j  a  v  a  2 s  . c  om*/
        prepareEnsuerIndex(id, r, data, isNew, updateIndexBatch, pList, null);
    } catch (Exception e) {
        resultHandler.handle(Future.failedFuture(e));
        return;
    }
    updateIndexBatch.executeAsync().addListener(f -> {
        resultHandler.handle(f.isSuccess() ? Future.succeededFuture()
                : Future.failedFuture(new RepositoryException(f.cause())));
        if (f.isSuccess()) {
            logger.debug("[" + getStorageKey() + "] Re Index Success : " + id);
        } else {
            logger.debug("[" + getStorageKey() + "] Re Index Failure", f.cause());
        }
    });
}

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 w w.  j ava  2  s . c o m*/
            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  w  w w  .  j av  a  2 s  .c  om
        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;/*from   w w w .jav  a  2  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  ww  .  j ava 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;
    }/* w ww  .  j a  v a2s.  co m*/
    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;
}