本章介绍 Vert.x 5版本的更新内容,帮助开发者将Vert.x 4.x应用升级到Vert.x 5,涵盖新特性、弃用功能及不兼容变更的详细信息。
1. 异步模型重构:全面转向Future编程
-
回调模型彻底移除 Vert.x 5删除了所有基于回调的API(如
Handler<AsyncResult<T>>
),需将所有旧版代码改造为基于Future
的链式调用。例如,原HTTP服务器监听代码需改写为:// Vert.x 3/4的回调写法 vertx.createHttpServer() .requestHandler(req -> req.response().end("Hello")) .listen(8080, res -> { if (res.succeeded()) { System.out.println("Server started"); } else { res.cause().printStackTrace(); } }); // Vert.x 5的Future写法 Future<HttpServer> future = vertx.createHttpServer()