• initMySQLPool


    package com.be.edge.asset.source;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Promise;
    import io.vertx.core.json.JsonObject;
    import io.vertx.mysqlclient.MySQLConnectOptions;
    import io.vertx.mysqlclient.MySQLPool;
    import io.vertx.sqlclient.PoolOptions;
    import io.vertx.sqlclient.Row;
    import io.vertx.sqlclient.RowSet;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class MySQLVerticle extends AbstractVerticle {
        private MySQLPool client;
    
        @Override
        public void start(Promise<Void> startPromise) throws Exception {
            initMySQLPool();
            /*
            client.query("SELECT * FROM data_list WHERE id = 1")
                .execute(ar -> {
                    if (ar.succeeded()) {
                        RowSet<Row> result = ar.result();
                        log.info("Got {} rows {}", result.size(), result);
                    } else {
                        log.info("Failure {}", ar.cause().getMessage());
                    }
                    client.close();
                });
            */
    
            client.getConnection().compose(conn -> {
                // All operations execute on the same connection
                return conn
                    .query("SELECT * FROM data_list WHERE id = 1")
                    .execute()
                    .compose(res -> conn
                        .query("SELECT * FROM data_list WHERE id = 2")
                        .execute())
                    .onComplete(ar -> {
                        // Release the connection to the pool
                        conn.close();
                    });
            }).onComplete(ar -> {
                if (ar.succeeded()) {
                    RowSet<Row> result = ar.result();
                    log.info("Got {} rows {}", result.rowCount(), result);
                    for (Row row : result) {
                        log.info("data {} {}", row.getInteger(0), row.getString(1));
                    }
                } else {
                    System.out.println("Something went wrong " + ar.cause().getMessage());
                }
            });
        }
    
        private void initMySQLPool() {
            JsonObject config = config();
            MySQLConnectOptions connectOptions = new MySQLConnectOptions(config.getJsonObject("connect"));
            PoolOptions poolOptions = new PoolOptions(config.getJsonObject("pool"));
            client = MySQLPool.pool(vertx, connectOptions, poolOptions);
        }
    }





  • 相关阅读:
    C#之设计模式
    C#之索引器
    C#基础强化-继承与多态
    C#基础强化-进程操作
    WKWebView与JavaScript交互基础
    HTML之JS学习
    HTML之CSS学习
    HTML学习
    玩诈欺的小杉
    最大异或和
  • 原文地址:https://www.cnblogs.com/exmyth/p/14248138.html
Copyright © 2020-2023  润新知