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.hijynx.ensemble.identity.UserServiceVertxEBProxy.java

License:Apache License

public UserService retrieveAllUsers(Handler<AsyncResult<List<User>>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from w  w w. jav a 2s  .  co  m*/
    JsonObject _json = new JsonObject();
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "retrieveAllUsers");
    _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().stream()
                    .map(o -> o instanceof Map ? new User(new JsonObject((Map) o)) : new User((JsonObject) o))
                    .collect(Collectors.toList())));
        }
    });
    return this;
}

From source file:io.hijynx.ensemble.identity.UserServiceVertxEBProxy.java

License:Apache License

public UserService updateUser(User user, Handler<AsyncResult<User>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from   ww w . jav a  2  s  . c o m*/
    JsonObject _json = new JsonObject();
    _json.put("user", user == null ? null : user.toJson());
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "updateUser");
    _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() == null ? null : new User(res.result().body())));
        }
    });
    return this;
}

From source file:io.hijynx.ensemble.identity.UserServiceVertxEBProxy.java

License:Apache License

public UserService deleteUser(String id, Handler<AsyncResult<Void>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/*from  ww w  .  j  a  va2s. co m*/
    JsonObject _json = new JsonObject();
    _json.put("id", id);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "deleteUser");
    _vertx.eventBus().<Void>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:io.hijynx.ensemble.identity.UserServiceVertxEBProxy.java

License:Apache License

public UserService updatePassword(String userId, String password, Handler<AsyncResult<Void>> resultHandler) {
    if (closed) {
        resultHandler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
        return this;
    }/* w w  w  .j a v a2 s  . c o  m*/
    JsonObject _json = new JsonObject();
    _json.put("userId", userId);
    _json.put("password", password);
    DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options)
            : new DeliveryOptions();
    _deliveryOptions.addHeader("action", "updatePassword");
    _vertx.eventBus().<Void>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:io.nonobot.core.chat.impl.ChatRouterImpl.java

License:Apache License

private void handle(io.vertx.core.eventbus.Message<JsonObject> message) {
    JsonObject body = message.body();// ww  w.j  av a  2  s.c o  m
    boolean respond = body.getBoolean("respond");
    String content = body.getString("content");
    String replyAddress = body.getString("replyAddress");
    String chatId = body.getString("chatId");
    for (MessageHandlerImpl handler : messageHandlers) {
        if (handler.respond == respond) {
            Matcher matcher = handler.pattern.matcher(content);
            if (matcher.matches()) {
                handler.handler.handle(new Message() {
                    boolean replied;

                    @Override
                    public String chatId() {
                        return chatId;
                    }

                    @Override
                    public String body() {
                        return content;
                    }

                    @Override
                    public String matchedGroup(int index) {
                        if (index > 0 && index <= matcher.groupCount()) {
                            return matcher.group(index);
                        } else {
                            return null;
                        }
                    }

                    @Override
                    public void reply(String msg) {
                        reply(msg, null);
                    }

                    @Override
                    public void reply(String msg, Handler<AsyncResult<Void>> ackHandler) {
                        reply(msg, DeliveryOptions.DEFAULT_TIMEOUT, ackHandler);
                    }

                    @Override
                    public void reply(String msg, long ackTimeout, Handler<AsyncResult<Void>> ackHandler) {
                        if (!replied) {
                            replied = true;
                            if (ackHandler != null) {
                                vertx.eventBus().send(replyAddress, msg,
                                        new DeliveryOptions().setSendTimeout(ackTimeout), ack -> {
                                            if (ack.succeeded()) {
                                                ackHandler.handle(Future.succeededFuture());
                                            } else {
                                                ackHandler.handle(Future.failedFuture(ack.cause()));
                                            }
                                        });
                            } else {
                                vertx.eventBus().send(replyAddress, msg);
                            }
                        } else if (ackHandler != null) {
                            ackHandler.handle(Future.failedFuture("Already replied"));
                        }
                    }
                });
                return;
            }
        }
    }
    message.reply(null);
}

From source file:io.nonobot.core.client.impl.BotClientImpl.java

License:Apache License

public BotClientImpl(Vertx vertx, Context context, ClientOptions options,
        Handler<AsyncResult<BotClient>> handler) {

    this.name = options.getName();
    this.inboundAddress = "bots." + name + ".inbound";
    this.outboundAddress = "bots." + name + ".outbound";

    // Default names
    alias(Arrays.asList(name, "@" + name));

    vertx.eventBus().<JsonObject>consumer(outboundAddress, msg -> {
        String chatId = msg.body().getString("chatId");
        String body = msg.body().getString("body");
        handle(new Message() {
            @Override/*www  .  ja va2  s .  com*/
            public String chatId() {
                return chatId;
            }

            @Override
            public String body() {
                return body;
            }
        });
    }).completionHandler(ar -> {
        if (ar.succeeded()) {
            handler.handle(Future.succeededFuture(this));
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    this.context = context;
    this.vertx = vertx;
}

From source file:io.nonobot.core.client.impl.BotClientImpl.java

License:Apache License

@Override
public void receiveMessage(ReceiveOptions options, String message, Handler<AsyncResult<String>> replyHandler) {
    String replyAddress = UUID.randomUUID().toString();
    Future<String> reply = Future.future();
    reply.setHandler(replyHandler);/*from w  ww  .ja  v  a  2 s.c o m*/
    MessageConsumer<String> consumer = vertx.eventBus().consumer(replyAddress);
    consumer.handler(msg -> {
        String content = msg.body();
        if (content != null && !reply.isComplete()) {
            if (msg.replyAddress() != null) {
                msg.reply(null);
            }
            reply.complete(content);
            consumer.unregister();
        } else {
            if (msg.replyAddress() != null) {
                msg.fail(0, "Already replied");
            }
        }
    });
    consumer.completionHandler(ar -> {
        if (ar.succeeded()) {
            Matcher botMatcher = botPattern.matcher(message);
            JsonObject msg = new JsonObject().put("replyAddress", replyAddress);
            msg.put("chatId", options.getChatId());
            if (botMatcher.find()) {
                msg.put("respond", true);
                msg.put("content", botMatcher.group(1));
            } else {
                msg.put("respond", false);
                msg.put("content", message);
            }
            vertx.eventBus().publish(inboundAddress, msg);
            vertx.setTimer(options.getTimeout(), timerID -> {
                if (!reply.isComplete()) {
                    consumer.unregister();
                    reply.fail(new Exception("timeout"));
                }
            });
        } else {
            replyHandler.handle(Future.failedFuture(ar.cause()));
        }
    });
}

From source file:io.nonobot.core.impl.BotImpl.java

License:Apache License

public static Bot createShared(Vertx vertx, BotOptions options, Handler<AsyncResult<Void>> completionHandler) {
    BotImpl bot = bots.computeIfAbsent(new Key(vertx, options.getName()),
            key -> new BotImpl(vertx, options.getName()));
    HttpServer server;/*from   w ww  .ja  va  2s.  c  o  m*/
    if (options.getHttpServerOptions() != null) {
        server = vertx.createHttpServer(options.getHttpServerOptions());
        server.requestHandler(bot.webRouter::accept);
        server.listen(ar -> {
            if (ar.succeeded()) {
                completionHandler.handle(Future.succeededFuture());
            } else {
                completionHandler.handle(Future.failedFuture(ar.cause()));
            }
        });
    } else {
        server = null;
        completionHandler.handle(Future.succeededFuture());
    }
    return new Bot() {
        @Override
        public Vertx vertx() {
            return bot.vertx();
        }

        @Override
        public ChatRouter chatRouter() {
            return bot.chatRouter();
        }

        @Override
        public Router webRouter() {
            return bot.webRouter();
        }

        @Override
        public String name() {
            return bot.name();
        }

        @Override
        public void close() {
            boolean open = bot.closed;
            bot.close();
            if (open && server != null) {
                server.close();
            }
        }
    };
}

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

License:Apache License

public void close() {
    if (closed) {
        throw new IllegalStateException("Connection pool already closed");
    }//w  w w. j a  va2s.  c o m
    closed = true;
    for (PooledConnection pooled : new ArrayList<>(all)) {
        pooled.close();
    }
    Future<Connection> failure = Future.failedFuture("Connection pool close");
    for (Future<Connection> pending : waiters) {
        try {
            pending.handle(failure);
        } catch (Exception ignore) {
        }
    }
}

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

License:Apache License

private <R1, R2 extends PgResultBase<R1, R2>, R3 extends PgResult<R1>> C preparedQuery(String sql,
        Tuple arguments, 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();
            String msg = ps.prepare((List<Object>) arguments);
            if (msg != null) {
                handler.handle(Future.failedFuture(msg));
            } else {
                PgResultBuilder<R1, R2, R3> b = new PgResultBuilder<>(factory, handler);
                cr.scheduler.schedule(new ExtendedQueryCommand<>(ps, arguments, singleton, collector, b), b);
            }//from w w w. j  a  va 2  s  .c  o  m
        } else {
            handler.handle(Future.failedFuture(cr.cause()));
        }
    });
    return (C) this;
}