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:io.reactiverse.pgclient.impl.PgClientBase.java

License:Apache License

private <R1, R2 extends PgResultBase<R1, R2>, R3 extends PgResult<R1>> C preparedBatch(String sql,
        List<Tuple> batch, boolean singleton, Function<R1, R2> factory, Collector<Row, ?, R1> collector,
        Handler<AsyncResult<R3>> handler) {
    schedule(new PrepareStatementCommand(sql), cr -> {
        if (cr.succeeded()) {
            PreparedStatement ps = cr.result();
            for (Tuple args : batch) {
                String msg = ps.prepare((List<Object>) args);
                if (msg != null) {
                    handler.handle(Future.failedFuture(msg));
                    return;
                }//from  w  w w  .  java 2  s. c o m
            }
            PgResultBuilder<R1, R2, R3> b = new PgResultBuilder<>(factory, handler);
            cr.scheduler.schedule(new ExtendedBatchQueryCommand<>(ps, batch, singleton, collector, b), b);
        } else {
            handler.handle(Future.failedFuture(cr.cause()));
        }
    });
    return (C) this;
}

From source file:io.reactiverse.pgclient.impl.PgConnectionBase.java

License:Apache License

public C prepare(String sql, Handler<AsyncResult<PgPreparedQuery>> handler) {
    schedule(new PrepareStatementCommand(sql), cr -> {
        if (cr.succeeded()) {
            handler.handle(Future.succeededFuture(new PgPreparedQueryImpl(conn, context, cr.result())));
        } else {//from   w w  w . j  a v  a  2  s.  co m
            handler.handle(Future.failedFuture(cr.cause()));
        }
    });
    return (C) this;
}

From source file:io.reactiverse.pgclient.impl.PgConnectionFactory.java

License:Apache License

public void connect(Handler<AsyncResult<SocketConnection>> handler) {
    switch (sslMode) {
    case DISABLE:
        doConnect(false, handler);//from w ww  . j  a v a 2 s .  c om
        break;
    case ALLOW:
        doConnect(false, ar -> {
            if (ar.succeeded()) {
                handler.handle(Future.succeededFuture(ar.result()));
            } else {
                doConnect(true, handler);
            }
        });
        break;
    case PREFER:
        doConnect(true, ar -> {
            if (ar.succeeded()) {
                handler.handle(Future.succeededFuture(ar.result()));
            } else {
                doConnect(false, handler);
            }
        });
        break;
    case VERIFY_FULL:
        if (hostnameVerificationAlgorithm == null || hostnameVerificationAlgorithm.isEmpty()) {
            handler.handle(Future.failedFuture(new IllegalArgumentException(
                    "Host verification algorithm must be specified under verify-full sslmode")));
            return;
        }
    case VERIFY_CA:
        if (trustOptions == null) {
            handler.handle(Future.failedFuture(new IllegalArgumentException(
                    "Trust options must be specified under verify-full or verify-ca sslmode")));
            return;
        }
    case REQUIRE:
        doConnect(true, handler);
        break;
    default:
        throw new IllegalArgumentException("Unsupported SSL mode");
    }
}

From source file:io.reactiverse.pgclient.impl.PgConnectionFactory.java

License:Apache License

private void doConnect(boolean ssl, Handler<AsyncResult<SocketConnection>> handler) {
    if (Vertx.currentContext() != ctx) {
        throw new IllegalStateException();
    }/*from w ww .  jav  a2 s .  c o m*/
    SocketAddress socketAddress;
    if (!isUsingDomainSocket) {
        socketAddress = SocketAddress.inetSocketAddress(port, host);
    } else {
        socketAddress = SocketAddress.domainSocketAddress(host + "/.s.PGSQL." + port);
    }

    Future<NetSocket> future = Future.<NetSocket>future().setHandler(ar -> {
        if (ar.succeeded()) {
            NetSocketInternal socket = (NetSocketInternal) ar.result();
            SocketConnection conn = newSocketConnection(socket);

            if (ssl && !isUsingDomainSocket) {
                // upgrade connection to SSL if needed
                conn.upgradeToSSLConnection(ar2 -> {
                    if (ar2.succeeded()) {
                        handler.handle(Future.succeededFuture(conn));
                    } else {
                        handler.handle(Future.failedFuture(ar2.cause()));
                    }
                });
            } else {
                handler.handle(Future.succeededFuture(conn));
            }
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    try {
        client.connect(socketAddress, null, future);
    } catch (Exception e) {
        // Client is closed
        future.fail(e);
    }
}

From source file:io.reactiverse.pgclient.impl.PgConnectionImpl.java

License:Apache License

@Override
public PgConnection cancelRequest(Handler<AsyncResult<Void>> handler) {
    Context current = Vertx.currentContext();
    if (current == context) {
        factory.connect(ar -> {//from w  ww.j av a2 s.co m
            if (ar.succeeded()) {
                SocketConnection conn = ar.result();
                conn.sendCancelRequestMessage(this.processId(), this.secretKey(), handler);
            } else {
                handler.handle(Future.failedFuture(ar.cause()));
            }
        });
    } else {
        context.runOnContext(v -> cancelRequest(handler));
    }
    return this;
}

From source file:io.reactiverse.pgclient.impl.PgPoolImpl.java

License:Apache License

@Override
public void begin(Handler<AsyncResult<PgTransaction>> handler) {
    getConnection(ar -> {/*from   w  w w. j a  va2 s  .c  om*/
        if (ar.succeeded()) {
            PgConnectionImpl conn = (PgConnectionImpl) ar.result();
            PgTransaction tx = conn.begin(true);
            handler.handle(Future.succeededFuture(tx));
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });
}

From source file:io.reactiverse.pgclient.impl.PgPreparedQueryImpl.java

License:Apache License

<A, R> PgPreparedQuery execute(Tuple args, int fetch, String portal, boolean suspended, boolean singleton,
        Collector<Row, A, R> collector, QueryResultHandler<R> resultHandler,
        Handler<AsyncResult<Boolean>> handler) {
    if (context == Vertx.currentContext()) {
        String msg = ps.prepare((List<Object>) args);
        if (msg != null) {
            handler.handle(Future.failedFuture(msg));
        } else {//from w w  w .j  a  v  a2s.  c om
            ExtendedQueryCommand cmd = new ExtendedQueryCommand<>(ps, args, fetch, portal, suspended, singleton,
                    collector, resultHandler);
            cmd.handler = handler;
            conn.schedule(cmd);
        }
    } else {
        context.runOnContext(
                v -> execute(args, fetch, portal, suspended, singleton, collector, resultHandler, handler));
    }
    return this;
}

From source file:io.reactiverse.pgclient.impl.PgPreparedQueryImpl.java

License:Apache License

private <R1, R2 extends PgResultBase<R1, R2>, R3 extends PgResult<R1>> PgPreparedQuery batch(
        List<Tuple> argsList, boolean singleton, Function<R1, R2> factory, Collector<Row, ?, R1> collector,
        Handler<AsyncResult<R3>> handler) {
    for (Tuple args : argsList) {
        String msg = ps.prepare((List<Object>) args);
        if (msg != null) {
            handler.handle(Future.failedFuture(msg));
            return this;
        }//from  w  ww . j  a  v  a2s . c  o m
    }
    PgResultBuilder<R1, R2, R3> b = new PgResultBuilder<>(factory, handler);
    ExtendedBatchQueryCommand cmd = new ExtendedBatchQueryCommand<>(ps, argsList, singleton, collector, b);
    cmd.handler = b;
    conn.schedule(cmd);
    return this;
}

From source file:io.reactiverse.pgclient.impl.PgPreparedQueryImpl.java

License:Apache License

@Override
public void close(Handler<AsyncResult<Void>> completionHandler) {
    if (closed.compareAndSet(false, true)) {
        CloseStatementCommand cmd = new CloseStatementCommand();
        cmd.handler = completionHandler;
        conn.schedule(cmd);/*from  ww w. j  a v  a 2  s. c om*/
    } else {
        completionHandler.handle(Future.failedFuture("Already closed"));
    }
}

From source file:io.reactiverse.pgclient.impl.SocketConnection.java

License:Apache License

void upgradeToSSLConnection(Handler<AsyncResult<Void>> completionHandler) {
    ChannelPipeline pipeline = socket.channelHandlerContext().pipeline();
    Future<Void> upgradeFuture = Future.future();
    upgradeFuture.setHandler(ar -> {/*from  ww  w  . ja  v  a2  s . co m*/
        if (ar.succeeded()) {
            completionHandler.handle(Future.succeededFuture());
        } else {
            Throwable cause = ar.cause();
            if (cause instanceof DecoderException) {
                DecoderException err = (DecoderException) cause;
                cause = err.getCause();
            }
            completionHandler.handle(Future.failedFuture(cause));
        }
    });
    pipeline.addBefore("handler", "initiate-ssl-handler", new InitiateSslHandler(this, upgradeFuture));
}