From ace5e10f771d45f9725e0f01d68a67389e5957c9 Mon Sep 17 00:00:00 2001 From: Julian Ladisch Date: Thu, 27 Nov 2025 13:33:26 +0100 Subject: [PATCH] Migrate from Vertx 4 to Vert.x 5 --- hello-vertx/pom.xml | 14 +++++--- .../java/org/folio/sample/MainVerticle.java | 36 +++++++++---------- simple-vertx/pom.xml | 14 +++++--- .../java/org/folio/simple/MainVerticle.java | 36 +++++++++---------- .../org/folio/simple/SimpleWebService.java | 28 +++++++-------- 5 files changed, 67 insertions(+), 61 deletions(-) diff --git a/hello-vertx/pom.xml b/hello-vertx/pom.xml index a8471e0..8a96dc0 100644 --- a/hello-vertx/pom.xml +++ b/hello-vertx/pom.xml @@ -26,14 +26,14 @@ io.vertx vertx-stack-depchain - 4.5.18 + 5.0.5 pom import org.apache.logging.log4j log4j-bom - 2.24.3 + 2.25.2 pom import @@ -45,6 +45,10 @@ io.vertx vertx-core + + io.vertx + vertx-launcher-application + io.vertx vertx-web @@ -52,7 +56,7 @@ org.folio.okapi okapi-common - 6.2.1 + 7.0.1 org.apache.logging.log4j @@ -68,7 +72,7 @@ maven-compiler-plugin - 3.14.0 + 3.14.1 21 -Xlint:unchecked @@ -135,7 +139,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.6.0 + 3.6.1 package diff --git a/hello-vertx/src/main/java/org/folio/sample/MainVerticle.java b/hello-vertx/src/main/java/org/folio/sample/MainVerticle.java index c21eca3..e13e35a 100644 --- a/hello-vertx/src/main/java/org/folio/sample/MainVerticle.java +++ b/hello-vertx/src/main/java/org/folio/sample/MainVerticle.java @@ -1,7 +1,7 @@ package org.folio.sample; -import io.vertx.core.AbstractVerticle; -import io.vertx.core.Promise; +import io.vertx.core.Future; +import io.vertx.core.VerticleBase; import io.vertx.ext.web.Router; import io.vertx.ext.web.RoutingContext; import java.lang.management.ManagementFactory; @@ -14,37 +14,35 @@ * The main verticle. This is the HTTP server that accepts incoming requests and * routes them to the relevant handlers. */ -public class MainVerticle extends AbstractVerticle { +public class MainVerticle extends VerticleBase { - private static final Logger logger = LogManager.getLogger(MainVerticle.class); + private static final Logger LOGGER = LogManager.getLogger(MainVerticle.class); @Override - public void start(Promise startPromise) { + public Future start() { - final int port = Integer.parseInt(System.getProperty("port", "8080")); - logger.info("Starting hello " - + ManagementFactory.getRuntimeMXBean().getName() - + " on port " + port); + var port = Integer.parseInt(System.getProperty("port", "8080")); + LOGGER.info("Starting hello {} on port {}", + ManagementFactory.getRuntimeMXBean().getName(), + port); // Define the routes for HTTP requests. - Router router = Router.router(vertx); + var router = Router.router(vertx); router.get("/hello").handler(this::getHandle); router.post("/hello").handler(this::postHandle); // And start listening - vertx.createHttpServer() - .requestHandler(router) - .listen(port) - .onSuccess(x -> logger.debug("Hello: Succeeded in starting the listener")) - .onFailure(e -> logger.error("Hello failed to start the listener:", e)) - .mapEmpty() - .onComplete(startPromise); + return vertx.createHttpServer() + .requestHandler(router) + .listen(port) + .onSuccess(x -> LOGGER.debug("Hello: Succeeded in starting the listener")) + .onFailure(e -> LOGGER.error("Hello failed to start the listener: {}", e.getMessage(), e)); } // Handler for the GET requests. // Just replies "Hello, world" in plain text public void getHandle(RoutingContext ctx) { - logger.debug("Hello: handling a GET request"); + LOGGER.debug("Hello: handling a GET request"); responseText(ctx, 200).end("Hello, world\n"); } @@ -52,7 +50,7 @@ public void getHandle(RoutingContext ctx) { // Replies with a JSON structure that contains all posted data // As long as the input data is valid JSON, the output should be too. public void postHandle(RoutingContext ctx) { - logger.debug("Hello: handling a POST request"); + LOGGER.debug("Hello: handling a POST request"); if (! "application/json".equals(ctx.request().getHeader("Content-Type"))) { responseError(ctx, 400, "Content-Type must be application/json"); return; diff --git a/simple-vertx/pom.xml b/simple-vertx/pom.xml index 6af5731..887b798 100644 --- a/simple-vertx/pom.xml +++ b/simple-vertx/pom.xml @@ -27,14 +27,14 @@ io.vertx vertx-stack-depchain - 4.5.18 + 5.0.5 pom import org.apache.logging.log4j log4j-bom - 2.24.3 + 2.25.2 pom import @@ -46,6 +46,10 @@ io.vertx vertx-core + + io.vertx + vertx-launcher-application + io.vertx vertx-web @@ -57,7 +61,7 @@ org.folio.okapi okapi-common - 6.2.1 + 7.0.1 org.apache.logging.log4j @@ -73,7 +77,7 @@ maven-compiler-plugin - 3.14.0 + 3.14.1 21 -Xlint:unchecked @@ -136,7 +140,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.6.0 + 3.6.1 package diff --git a/simple-vertx/src/main/java/org/folio/simple/MainVerticle.java b/simple-vertx/src/main/java/org/folio/simple/MainVerticle.java index 2e3a5a4..337e52f 100644 --- a/simple-vertx/src/main/java/org/folio/simple/MainVerticle.java +++ b/simple-vertx/src/main/java/org/folio/simple/MainVerticle.java @@ -1,7 +1,9 @@ package org.folio.simple; import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; import io.vertx.core.Promise; +import io.vertx.core.VerticleBase; import io.vertx.core.http.HttpClient; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; @@ -13,12 +15,12 @@ * The main verticle. This is the HTTP server that accepts incoming requests and * routes them to the relevant handlers. */ -public class MainVerticle extends AbstractVerticle { +public class MainVerticle extends VerticleBase { - private static final Logger logger = LogManager.getLogger(MainVerticle.class); + private static final Logger LOGGER = LogManager.getLogger(MainVerticle.class); @Override - public void start(Promise startPromise) { + public Future start() { /** * All services of one Verticle instance should share a single HttpClient (or WebClient) * to allow for HTTP pooling and HTTP pipe-lining and to avoid HttpClient socket leaks. @@ -26,25 +28,23 @@ public void start(Promise startPromise) { final HttpClient httpClient = vertx.createHttpClient(); final SimpleWebService simple = new SimpleWebService(httpClient); - final int port = Integer.parseInt(System.getProperty("port", "8080")); - logger.info("Starting simple " - + ManagementFactory.getRuntimeMXBean().getName() - + " on port " + port); + var port = Integer.parseInt(System.getProperty("port", "8080")); + LOGGER.info("Starting simple {} on port {}", + ManagementFactory.getRuntimeMXBean().getName(), + port); // Define the routes for HTTP requests. Both GET and POST go to the same // one here... - Router router = Router.router(vertx); - router.get("/simple").handler(simple::get_handle); - router.post("/*").handler(BodyHandler.create()); // Tell vertx we want to the whole POST body in the handler - router.post("/simple").handler(simple::post_handle); + var router = Router.router(vertx); + router.get("/simple").handler(simple::getHandle); + router.post("/*").handler(BodyHandler.create()); // Tell vertx we want the whole POST body in the handler + router.post("/simple").handler(simple::postHandle); // And start listening - vertx.createHttpServer() - .requestHandler(router) - .listen(port) - .onSuccess(x -> logger.debug("Succeeded in starting the listener for simple")) - .onFailure(e -> logger.error("simple failed: " + e.getMessage(), e)) - .mapEmpty() - .onComplete(startPromise); + return vertx.createHttpServer() + .requestHandler(router) + .listen(port) + .onSuccess(x -> LOGGER.debug("Succeeded in starting the listener for simple")) + .onFailure(e -> LOGGER.error("simple failed: {}", e.getMessage(), e)); } } diff --git a/simple-vertx/src/main/java/org/folio/simple/SimpleWebService.java b/simple-vertx/src/main/java/org/folio/simple/SimpleWebService.java index 94556ed..eb9c1bd 100644 --- a/simple-vertx/src/main/java/org/folio/simple/SimpleWebService.java +++ b/simple-vertx/src/main/java/org/folio/simple/SimpleWebService.java @@ -17,7 +17,7 @@ * @author heikki */ public class SimpleWebService { - private static final Logger logger = LogManager.getLogger(SimpleWebService.class); + private static final Logger LOGGER = LogManager.getLogger(SimpleWebService.class); private final HttpClient httpClient; public SimpleWebService(HttpClient httpClient) { @@ -26,10 +26,10 @@ public SimpleWebService(HttpClient httpClient) { // Handler for the GET requests. // Calls the hello module, and reports its success - public void get_handle(RoutingContext ctx) { - logger.debug("Simple: Handling a GET request. About to call the hello module"); - OkapiClient okapiClient = new OkapiClient(httpClient, ctx); - logger.debug("Contacting Okapi via " + okapiClient.getOkapiUrl()); + public void getHandle(RoutingContext ctx) { + LOGGER.debug("Simple: Handling a GET request. About to call the hello module"); + var okapiClient = new OkapiClient(httpClient, ctx); + LOGGER.debug("Contacting Okapi via {}", okapiClient.getOkapiUrl()); okapiClient.get("/hello") .onSuccess(body -> responseText(ctx, 200).end("Hello module says: '" + body + "'")) .onFailure(e -> responseError(ctx, 500, "Hello failed with " + e.getMessage())); @@ -38,24 +38,24 @@ public void get_handle(RoutingContext ctx) { // Handler for the POST request // POSTs the same request to the hello module, and returns its response in a // new Json response - public void post_handle(RoutingContext ctx) { + public void postHandle(RoutingContext ctx) { if (! "application/json".equals(ctx.request().getHeader("Content-Type"))) { responseError(ctx, 400, "Only accepts Content-Type application/json"); return; } - String reqData = ctx.getBodyAsString(); - logger.debug("Simple: Received a POST of " + reqData); - OkapiClient okapiClient = new OkapiClient(httpClient, ctx); + var reqData = ctx.body().asString(); + LOGGER.debug("Simple: Received a POST of {}", reqData); + var okapiClient = new OkapiClient(httpClient, ctx); okapiClient.setHeaders(Map.of( "Content-Type", "application/json", "X-Okapi-Tenant", ctx.request().getHeader("X-Okapi-Tenant"))); okapiClient.post("/hello", reqData) .onSuccess(helloRes -> { - JsonObject hjo = new JsonObject(helloRes); - String greeting = hjo.getString("greeting"); - logger.info("Got a greeting from hello: " + greeting); - hjo.put("simple", "Simple module did indeed call the hello module!"); - String simpleRes = hjo.encodePrettily(); + var jsonObject = new JsonObject(helloRes); + var greeting = jsonObject.getString("greeting"); + LOGGER.info("Got a greeting from hello: {}", greeting); + jsonObject.put("simple", "Simple module did indeed call the hello module!"); + var simpleRes = jsonObject.encodePrettily(); responseJson(ctx, 200).end(simpleRes); }) .onFailure(e -> responseError(ctx, 500, "Hello failed with " + e.getMessage()));