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.jedlab.vertee.DatabaseServiceVertxEBProxy.java

License:Apache License

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

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private <T> T wrapInTCCLSwitch(Callable<T> callable, Handler<AsyncResult<FileProps>> resultHandler) {
    try {//from w  w  w .j av a  2  s.c  o  m
        if (classLoader == null) {
            return callable.call();
        } else {
            final ClassLoader original = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(classLoader);
                return callable.call();
            } finally {
                Thread.currentThread().setContextClassLoader(original);
            }
        }
    } catch (Exception e) {
        if (resultHandler != null) {
            resultHandler.handle(Future.failedFuture(e.getCause()));
            return null;
        } else {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java

License:Open Source License

private synchronized void getFileProps(RoutingContext context, String file,
        Handler<AsyncResult<FileProps>> resultHandler) {
    // FileSystem fs = context.vertx().fileSystem();
    FileSystem fs = new WindowsFileSystem((VertxInternal) context.vertx());
    if (alwaysAsyncFS || useAsyncFS) {
        wrapInTCCLSwitch(() -> fs.props(file, resultHandler), resultHandler);
    } else {/*from w  w  w.  j av a2 s. c  o m*/
        // Use synchronous access - it might well be faster!
        long start = 0;
        if (tuning) {
            start = System.nanoTime();
        }
        try {
            FileProps props = wrapInTCCLSwitch(() -> fs.propsBlocking(file), resultHandler);

            if (tuning) {
                long end = System.nanoTime();
                long dur = end - start;
                totalTime += dur;
                numServesBlocking++;
                if (numServesBlocking == Long.MAX_VALUE) {
                    // Unlikely.. but...
                    resetTuning();
                } else if (numServesBlocking == nextAvgCheck) {
                    double avg = (double) totalTime / numServesBlocking;
                    if (avg > maxAvgServeTimeNanoSeconds) {
                        useAsyncFS = true;
                        log.info(
                                "Switching to async file system access in static file server as fs access is slow! (Average access time of "
                                        + avg + " ns)");
                        tuning = false;
                    }
                    nextAvgCheck += NUM_SERVES_TUNING_FS_ACCESS;
                }
            }
            resultHandler.handle(Future.succeededFuture(props));
        } catch (FileSystemException e) {
            resultHandler.handle(Future.failedFuture(e.getCause()));
        }
    }
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService save(JsonObject payload, Handler<AsyncResult<JsonArray>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from   ww  w . j  a v a 2  s .  c o  m*/
    JsonObject _json = new JsonObject();
    _json.put("payload", payload);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "save");
    _vertx.eventBus().<JsonArray>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.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService query(String sql, Handler<AsyncResult<List<JsonObject>>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from ww  w.  ja va  2  s .co m
    JsonObject _json = new JsonObject();
    _json.put("sql", sql);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "query");
    _vertx.eventBus().<JsonArray>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            resultHandler.handle(Future.failedFuture(res.cause()));
        } else {
            resultHandler.handle(Future.succeededFuture(convertList(res.result().body().getList())));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService queryOne(String sql, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from w ww.j a  v a 2s. c  om*/
    JsonObject _json = new JsonObject();
    _json.put("sql", sql);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "queryOne");
    _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()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService one(Long id, Handler<AsyncResult<JsonObject>> 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("id", id);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "one");
    _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()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService findByUsername(String username, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*  w w w. j av a  2s.c o m*/
    JsonObject _json = new JsonObject();
    _json.put("username", username);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "findByUsername");
    _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()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public UserService findByEmail(String email, Handler<AsyncResult<JsonObject>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }//from  w ww .jav a2  s . co  m
    JsonObject _json = new JsonObject();
    _json.put("email", email);
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "findByEmail");
    _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()));
        }
    });
    return this;
}

From source file:com.panjiesw.std.service.user.UserServiceVertxEBProxy.java

License:Apache License

public void stop(Handler<AsyncResult<Void>> whenDone) {
    if (closed) {
        whenDone.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return;//from w w w  .j  a v a  2s  .c om
    }
    closed = true;
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = new DeliveryOptions();
    _deliveryOptions.addHeader("action", "stop");
    _vertx.eventBus().<Void>send(_address, _json, _deliveryOptions, res -> {
        if (res.failed()) {
            whenDone.handle(Future.failedFuture(res.cause()));
        } else {
            whenDone.handle(Future.succeededFuture(res.result().body()));
        }
    });
}