使用 Vert.x 开发非阻塞的TCP服务端非常方便,使用Vertx对象创建HTTPServer即可。

HTTPServer

通过Vertx对象创建HTTPServer,并监听指定的8080端口:

public class HTTPStarter {

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
      req.response().end("Hi Vert.x!");
    });
    server.listen(8080, res -> {
      if (res.succeeded()) {
        System.out.println("HTTP: " + res.result().actualPort());
      } else {
        System.out.println("HTTP: " + res.cause().getMessage());
      }
    });
  }
}

测试:

Vert.x - Core HTTPServer


彩蛋:关注公众号、或小程序,阅读更多IT文章。

Vert.x - Core HTTPServer

相关文章:

  • 2021-10-11
  • 2021-10-04
  • 2021-04-17
  • 2021-07-02
  • 2022-01-19
  • 2022-12-23
猜你喜欢
  • 2021-12-30
  • 2021-07-31
  • 2021-11-29
  • 2021-09-16
  • 2021-12-03
  • 2021-10-04
  • 2021-10-06
相关资源
相似解决方案