Example usage for io.vertx.core Future future

List of usage examples for io.vertx.core Future future

Introduction

In this page you can find the example usage for io.vertx.core Future future.

Prototype

future

Source Link

Usage

From source file:com.dinstone.vertx.web.resource.UserResource.java

License:Apache License

@Post("/bpf")
public Future<UserBean> bpf(@BeanParam UserBean user) {
    Future<UserBean> future = Future.future();
    future.complete(user);//from  ww w .j av  a 2s.c o  m
    return future;
}

From source file:com.englishtown.vertx.hk2.HK2VerticleLoader.java

License:Open Source License

/**
 * Vert.x calls the stop method when the verticle is undeployed.
 * Put any cleanup code for your verticle in here
 *
 * @throws Exception//from  w  w w.  j a  va  2 s.c om
 */
@Override
public void stop(Future<Void> stopFuture) throws Exception {

    classLoader = null;
    parent = null;

    Future<Void> future = Future.future();
    future.setHandler(result -> {
        // Destroy the service locator
        ServiceLocatorFactory.getInstance().destroy(locator);
        locator = null;

        // Pass result to the stop future
        if (result.succeeded()) {
            stopFuture.complete();
        } else {
            stopFuture.fail(future.cause());
        }
    });

    try {
        // Stop the real verticle
        if (realVerticle != null) {
            realVerticle.stop(future);
        } else {
            future.complete();
        }
    } catch (Throwable t) {
        future.fail(t);
    }

}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {/*from  w ww.  j  av a2s.  c om*/
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                jrs.put("data", r.result().getRows().stream().map(Driver::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Optional<Driver>> getOne(String id) {
    Future<Optional<Driver>> result = Future.future();
    client.getConnection(connHandler(result, connection -> {
        connection.queryWithParams(SQL_QUERY, new JsonArray().add(id), r -> {
            if (r.failed()) {
                result.fail(r.cause());//www  . ja va  2s. c  om
            } else {
                List<JsonObject> list = r.result().getRows();
                if (list == null || list.isEmpty()) {
                    result.complete(Optional.empty());
                } else {
                    result.complete(Optional.of(new Driver(list.get(0))));
                }
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Optional<Driver>> insert(JsonObject json) {
    Future<Optional<Driver>> result = Future.future();
    JsonArray params = new JsonArray();
    //username,email,phone,driverlicense,memo,status
    params.add(json.getString("username"));
    params.add(json.getString("email"));
    params.add(json.getString("phone"));
    params.add(json.getString("driverlicense"));
    params.add(json.getString("memo"));
    params.add(json.getString("status"));
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(SQL_INSERT, params, r -> {
            if (r.failed()) {
                result.fail(r.cause());//w ww .  j  av  a 2  s . c o  m
            } else {
                UpdateResult urs = r.result();
                params.clear();
                params.add(urs.getKeys().getInteger(0));
                log.info(urs.getKeys().getInteger(0));
                connection.queryWithParams(SQL_QUERY, params, rs -> {
                    if (rs.failed()) {
                        result.fail(rs.cause());
                        log.info(rs.cause());
                    } else {
                        List<JsonObject> list = rs.result().getRows();
                        if (list == null || list.isEmpty()) {
                            result.complete(Optional.empty());
                        } else {
                            result.complete(Optional.of(new Driver(list.get(0))));
                        }
                    }
                });
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

@Override
public Future<Boolean> update(JsonObject params) {
    Future<Boolean> result = Future.future();
    int id = params.getInteger("id");
    JsonArray setparams = new JsonArray();
    StringBuffer sqlSet = new StringBuffer();
    params.forEach(r -> {//from  www  .  j ava  2  s .co m
        if (!r.getKey().equals("id")) {
            sqlSet.append(r.getKey()).append("=?,");
            setparams.add(r.getValue());
        }
    });
    if (sqlSet.length() > 0) {
        sqlSet.deleteCharAt(sqlSet.length() - 1);
    } else {
        sqlSet.append("status=abs(status-1)");
    }
    sqlSet.append(" where id = ?");
    setparams.add(id);
    String sql = SQL_UPDATE + sqlSet.toString();
    log.info(sql);
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(sql, setparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                result.complete(true);
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.DriverServiceImpl.java

private Future<Boolean> deleteProcess(String sql, JsonArray params) {
    Future<Boolean> result = Future.future();
    client.getConnection(connHandler(result, connection -> connection.updateWithParams(sql, params, r -> {
        if (r.failed()) {
            result.complete(false);//ww w .j a  v  a2  s.  c om
        } else {
            result.complete(true);
        }
        connection.close();
    })));
    return result;
}

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<JsonObject> getAll(JsonObject params) {
    Future<JsonObject> result = Future.future();
    StringBuffer sqlCount = new StringBuffer(SQL_QUERY_COUNT);
    StringBuffer sqlQuery = new StringBuffer(SQL_QUERY_ALL);
    StringBuffer order = new StringBuffer();
    StringBuffer where = new StringBuffer();
    JsonArray queryparams = new JsonArray();
    JsonArray countparams = new JsonArray();
    params.forEach(r -> {/*from   w  ww.j  av  a  2 s . c  om*/
        String key = r.getKey();
        if (key.equals("orderby")) {
            order.append(" order by ").append(r.getValue());
        } else if (key.equals("page")) {
        } else if (key.equals("page_size")) {
        } else {
            if (where.length() == 0)
                where.append(" where ");
            else
                where.append(" and ");
            where.append(key).append(" like ? ");
            queryparams.add("%" + r.getValue() + "%");
            countparams.add("%" + r.getValue() + "%");
        }
    });

    if (where.length() > 0) {
        sqlCount.append(where);
        sqlQuery.append(where);
    }
    JsonObject jrs = new JsonObject();
    client.getConnection(connHandler(result, connection -> {
        log.info(sqlCount);
        if (countparams.size() == 0)
            connection.query(sqlCount.toString(), r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        else
            connection.queryWithParams(sqlCount.toString(), countparams, r -> {
                if (r.failed()) {
                    result.fail(r.cause());
                } else {
                    int count = r.result().getRows().get(0).getInteger("count");
                    jrs.put("total", count);
                }
            });
        if (order.length() > 0) {
            sqlQuery.append(order);
            queryparams.add(params.getString("orderby"));
        }
        if (params.getValue("page") == null) {
            params.put("page", "0");
            params.put("page_size", "" + Integer.MAX_VALUE);
        }
        sqlQuery.append(" limit ?,? ");
        log.info(sqlQuery);
        int page = Integer.parseInt(params.getString("page"));
        int limit = Integer.parseInt(params.getString("page_size"));
        queryparams.add(Utils.calcPage(page, limit)).add(limit);
        connection.queryWithParams(sqlQuery.toString(), queryparams, r -> {
            if (r.failed()) {
                result.fail(r.cause());
            } else {
                log.info(r.result().getRows().toString());
                jrs.put("data", r.result().getRows().stream().map(Line::new).collect(Collectors.toList()));
                jrs.put("per_page", limit);
                jrs.put("current_page", page);
            }
            result.complete(new JsonObject().put("status", 200).put("data", new JsonObject().put("list", jrs)));
        });
        connection.close();

    }));
    return result;
}

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<Optional<Line>> getOne(String id) {
    Future<Optional<Line>> result = Future.future();
    client.getConnection(connHandler(result, connection -> {
        connection.queryWithParams(SQL_QUERY, new JsonArray().add(id), r -> {
            if (r.failed()) {
                result.fail(r.cause());//w w w.j  a  v  a 2s  .  c o  m
            } else {
                List<JsonObject> list = r.result().getRows();
                if (list == null || list.isEmpty()) {
                    result.complete(Optional.empty());
                } else {
                    result.complete(Optional.of(new Line(list.get(0))));
                }
            }
            connection.close();
        });
    }));
    return result;
}

From source file:com.funmix.service.LineServiceImpl.java

@Override
public Future<Optional<Line>> insert(JsonObject json) {
    log.info(json.toString());// w  w  w.j  ava 2s .  c o m
    Future<Optional<Line>> result = Future.future();
    JsonArray params = new JsonArray();
    // licenseplate,drivername,startaddr,starttime,endtime,endaddr,linename,path
    params.add(json.getString("licenseplate"));
    params.add(json.getString("drivername"));
    params.add(json.getString("startaddr"));
    params.add(json.getString("starttime"));
    params.add(json.getString("endtime"));
    params.add(json.getString("endaddr"));
    params.add(json.getString("linename"));
    params.add(json.getJsonArray("path").toString());
    StringBuffer updateOrder = new StringBuffer("update torder set status=2 where orderno in (");
    JsonArray setparams = new JsonArray();
    if (json.getValue("orderlist") != null) {
        JsonArray ja = json.getJsonArray("orderlist");
        ja.forEach(r -> {
            updateOrder.append("?,");
            setparams.add(r);
        });
        updateOrder.deleteCharAt(updateOrder.length() - 1).append(")");
    }
    log.info(setparams.toString());
    log.info(params.toString());
    client.getConnection(connHandler(result, connection -> {
        connection.updateWithParams(updateOrder.toString(), setparams, ru -> {
            if (ru.failed()) {
                result.fail(ru.cause());
                log.info(ru.cause());
            } else {
                connection.updateWithParams(SQL_INSERT, params, r -> {
                    if (r.failed()) {
                        result.fail(r.cause());
                        log.info(r.cause());
                    } else {
                        UpdateResult urs = r.result();
                        params.clear();
                        params.add(urs.getKeys().getInteger(0));
                        log.info(urs.getKeys().getInteger(0));
                        connection.queryWithParams(SQL_QUERY, params, rs -> {
                            if (rs.failed()) {
                                result.fail(rs.cause());
                                log.info(rs.cause());
                            } else {
                                List<JsonObject> list = rs.result().getRows();
                                if (list == null || list.isEmpty()) {
                                    result.complete(Optional.empty());
                                } else {
                                    result.complete(Optional.of(new Line(list.get(0))));
                                }
                            }
                        });
                    }
                    connection.close();
                });
            }
        });

    }));
    return result;
}