【Vert.x】初见

1,基于Netty的异步框架,采用事件机制,这点同android消息机制类似,在此学习简单的使用

2,例子

package com.zjw.demo.vertx.starter;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;


/**
 * 简单使用,
 * 参考<a href="https://vertx.io/docs">...</a>
 */

public class VertxApp {
  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    HttpServer httpServer = vertx.createHttpServer();

    /*创建应用路由*/
    Router router = Router.router(vertx);

    /*两种get方法方式*/
    //1
    router.route(HttpMethod.GET, "/test1").handler(ctx -> ctx.end("this is async handler"));

    //2
    router.get("/path/get1").respond(ctx -> Future.succeededFuture(new JsonObject().put("/path/get1", "hello vert.x")));

    router.get("/path/get2").blockingHandler(ctx -> {
      ctx.response().setChunked(true).write("this is block handler,wait 2 second");
      //取消阻塞,
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {

      }
      ctx.next();
    }).handler(ctx -> ctx.response().end("\nnow,2 second after"));


    /*path参数*/
    router.get("/path/get3/:p1/:p2").handler(ctx -> {
      String p1 = ctx.pathParam("p1");
      String p2 = ctx.pathParam("p2");
      ctx.end(p1 + "+" + p2 + "=" + (Integer.parseInt(p1) + Integer.parseInt(p2)));
    });


    router.route("/path/s1").method(HttpMethod.POST).method(HttpMethod.PUT).handler(ctx -> {
      //这个路由可以对应post或者put请求
    });


    router.route("/pata/data/01").method(HttpMethod.GET).handler(ctx -> {
      new MysqlConnect().init(vertx);

      ctx.end("查询数据库");
    });


    /*设置路由,监听8888端口*/
    httpServer.requestHandler(router).listen(8888, http -> {
      if (http.succeeded()) {
        /*监听成功*/
        System.out.println("HTTP server started on port 8888");
      } else {
        /*监听失败*/
        System.err.println("HTTP server error");
      }
    });
  }
}

数据库相关,

package com.zjw.demo.vertx.starter;

import io.vertx.core.Vertx;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.*;

public class MysqlConnect {

  public void init(Vertx vertx) {
    MySQLConnectOptions connectOptions = new MySQLConnectOptions()
      .setPort(3306)
      .setHost("127.0.0.1")
      .setDatabase("dataBaseName")
      .setUser("user")
      .setPassword("password");

    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    SqlClient client = MySQLPool.client(connectOptions, poolOptions);

    /*
     * 简单查询
     * */
    client.query("SELECT * FROM users WHERE 0=0 AND id = '12'")
      .execute(ar -> {
        if (ar.succeeded()) {
          RowSet<Row> result = ar.result();
          System.out.println(result);
        } else {
          System.err.println("Failed:" + ar.cause().getMessage());
        }
      });


    /*
     * 简单插入
     * */
    client
      .preparedQuery("INSERT INTO users (first_name, last_name) VALUES (?, ?)")
      .execute(Tuple.of("Julien", "Viet"), ar -> {
        if (ar.succeeded()) {
          RowSet<Row> rows = ar.result();
          System.out.println(rows.rowCount());
        } else {
          System.out.println("Failure: " + ar.cause().getMessage());
        }
      });

    //关闭
    client.close();
  }
}

3,源码分析

...

4,基于vertx的类spring异步框架

vert_x_base_service: vert.x service框架base

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值