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.test.db.DbServiceVertxEBProxy.java

License:Apache License

public void read(String query, JsonObject databaseConfig, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from   w w w .  ja v a2 s .co  m
    }
    JsonObject _json = new JsonObject();
    _json.put("query", query);
    _json.put("databaseConfig", databaseConfig);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "read");
    _vertx.eventBus().<JsonObject>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.test.db.DbServiceVertxEBProxy.java

License:Apache License

public void nonSharedRead(String query, JsonObject databaseConfig,
        Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from w ww  .  j av a 2 s  .  c  o m*/
    }
    JsonObject _json = new JsonObject();
    _json.put("query", query);
    _json.put("databaseConfig", databaseConfig);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "nonSharedRead");
    _vertx.eventBus().<JsonObject>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.test.mailer.MailerServiceVertxEBProxy.java

License:Apache License

public void sendAttachment(String To, MailAttachment attachment, String Title,
        Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;/*from  w  ww  .j a v  a2s  .com*/
    }
    JsonObject _json = new JsonObject();
    _json.put("To", To);
    _json.put("attachment", attachment == null ? null : attachment.toJson());
    _json.put("Title", Title);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "sendAttachment");
    _vertx.eventBus().<JsonObject>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.usebilbo.vertx.cluster.manager.impl.AsyncMapImpl.java

License:Open Source License

/**
 * @param ttl/*from   w  w  w . jav a 2 s .  co  m*/
 *            Time to live in ms.
 */
private <T> void executeWithTtl(Consumer<IgniteCache<K, V>> cacheOp, Handler<AsyncResult<T>> handler,
        long ttl) {
    try {
        @SuppressWarnings("resource")
        IgniteCache<K, V> cache0 = ttl > 0
                ? cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl)))
                : cache;
        cacheOp.accept(cache0);

        IgniteFuture<T> future = cache0.future();
        future.listen(fut -> vertx.executeBlocking(f -> f.complete(future.get()), handler));
    } catch (Exception e) {
        handler.handle(Future.failedFuture(e));
    }
}

From source file:com.usebilbo.vertx.cluster.manager.impl.AsyncMultiMapImpl.java

License:Open Source License

private <T, R> void execute(Consumer<IgniteCache<K, List<V>>> cacheOp, Function<T, R> mapper,
        Handler<AsyncResult<R>> handler) {
    try {/* w w  w .  j  av  a 2  s  . c  o m*/
        cacheOp.accept(cache);
        IgniteFuture<T> future = cache.future();
        future.listen(fut -> vertx.getOrCreateContext()
                .executeBlocking(f -> f.complete(mapper.apply(future.get())), handler));
    } catch (Exception e) {
        handler.handle(Future.failedFuture(e));
    }
}

From source file:de.braintags.netrelay.controller.api.DataTablesController.java

License:Open Source License

private void execute(IQuery<?> query, DataTableLinkDescriptor descr, long tableCount,
        Handler<AsyncResult<JsonObject>> handler) {
    query.execute(qr -> {/*  w  ww  . j  a va2  s.c o m*/
        if (qr.failed()) {
            handler.handle(Future.failedFuture(qr.cause()));
        } else {
            LOGGER.info(qr.result().getOriginalQuery());
            long totalResult = qr.result().getCompleteResult();
            qr.result().toArray(result -> {
                if (result.failed()) {
                    handler.handle(Future.failedFuture(result.cause()));
                } else {
                    Object[] selection = result.result();
                    LOGGER.info("SELECTION SIZE: " + selection.length);
                    if (selection.length == 0) {
                        handler.handle(Future.succeededFuture(createJsonObject(query.getMapper(),
                                new ArrayList<IStoreObject<?, ?>>(), descr, totalResult, tableCount)));
                    } else {
                        IStoreObjectFactory<?> sf = mapperFactory.getStoreObjectFactory();
                        sf.createStoreObjects(query.getMapper(), Arrays.asList(selection), str -> {
                            if (str.failed()) {
                                handler.handle(Future.failedFuture(result.cause()));
                            } else {
                                List tmpSel = str.result();
                                JsonObject jo = createJsonObject(query.getMapper(), tmpSel, descr, totalResult,
                                        tableCount);
                                handler.handle(Future.succeededFuture(jo));
                            }
                        });
                    }
                }
            });
        }
    });
}

From source file:de.braintags.netrelay.controller.api.MailController.java

License:Open Source License

/**
 * @param context/*  w  ww . j  av  a2s  .  c  o  m*/
 * @param prefs
 * @param handler
 * @param email
 * @param template
 */
private static void createMailContent(RoutingContext context, MailPreferences prefs,
        Handler<AsyncResult<MailMessage>> handler, MailMessage email, String template) {
    if (template != null && template.hashCode() != 0) {
        String file = prefs.templateDirectory + "/" + template;
        prefs.templateEngine.render(context, file, res -> {
            if (res.succeeded()) {
                email.setHtml(res.result().toString());
                checkInlineMessage(context, prefs, email, handler);
            } else {
                handler.handle(Future.failedFuture(res.cause()));
            }
        });
    } else {
        String html = prefs.html == null || prefs.html.hashCode() == 0
                ? readParameterOrContext(context, HTML_PARAMETER, null, false)
                : prefs.html;
        email.setHtml(html);
        checkInlineMessage(context, prefs, email, handler);
    }
}

From source file:de.braintags.netrelay.controller.api.MailController.java

License:Open Source License

private static void readData(MailPreferences prefs, UriMailAttachment attachment,
        Handler<AsyncResult<Void>> handler) {
    URI uri = attachment.getUri();
    HttpClient client = prefs.httpClient;
    int port = uri.getPort() > 0 ? uri.getPort() : 80;
    HttpClientRequest req = client.request(HttpMethod.GET, port, uri.getHost(), uri.getPath(), resp -> {
        resp.bodyHandler(buff -> {/*w w w . j  a v  a2s.  c  om*/
            try {
                attachment.setData(buff);
                handler.handle(Future.succeededFuture());
            } catch (Exception e) {
                LOGGER.error("", e);
                handler.handle(Future.failedFuture(e));
            }
        });
    });
    req.end();
}

From source file:de.braintags.netrelay.controller.authentication.PasswordLostController.java

License:Open Source License

private void deactivatePreviousClaims(RoutingContext context, String email,
        Handler<AsyncResult<Void>> handler) {
    IQuery<PasswordLostClaim> query = getNetRelay().getDatastore().createQuery(PasswordLostClaim.class);
    query.field("email").is(email).field("active").is(true);
    QueryHelper.executeToList(query, qr -> {
        if (qr.failed()) {
            handler.handle(Future.failedFuture(qr.cause()));
        } else {//from  ww w . j a va 2s .  c  o  m
            List<PasswordLostClaim> cl = (List<PasswordLostClaim>) qr.result();
            if (!cl.isEmpty()) {
                IWrite<PasswordLostClaim> write = getNetRelay().getDatastore()
                        .createWrite(PasswordLostClaim.class);
                cl.forEach(rc -> rc.setActive(false));
                write.addAll(cl);
                write.save(wr -> {
                    if (wr.failed()) {
                        handler.handle(Future.failedFuture(wr.cause()));
                    } else {
                        handler.handle(Future.succeededFuture());
                    }
                });
            } else {
                handler.handle(Future.succeededFuture());
            }
        }
    });
}

From source file:de.braintags.netrelay.controller.authentication.PasswordLostController.java

License:Open Source License

private void createPasswordLostClaim(RoutingContext context, String email,
        Handler<AsyncResult<PasswordLostClaim>> handler) {
    deactivatePreviousClaims(context, email, previous -> {
        if (previous.failed()) {
            handler.handle(Future.failedFuture(previous.cause()));
        } else {/*  w w  w.j  a  v  a  2 s.co m*/
            PasswordLostClaim rc = new PasswordLostClaim(email, context.request());
            IWrite<PasswordLostClaim> write = getNetRelay().getDatastore().createWrite(PasswordLostClaim.class);
            write.add(rc);
            write.save(sr -> {
                if (sr.failed()) {
                    LOGGER.error("", sr.cause());
                    handler.handle(Future.failedFuture(sr.cause()));
                } else {
                    context.put(PasswordLostClaim.class.getSimpleName(), rc);
                    context.put(MailController.TO_PARAMETER, email);
                    context.put(VALIDATION_ID_PARAM, rc.id);
                    handler.handle(Future.succeededFuture(rc));
                }
            });
        }
    });
}