vertx-integrate-spring-boot

Example of Vert.x integration with Spring Boot

  • Vert.x
    • Router
    • vertx.eventBus()
  • Spring Boot

Project Structure

Deploying verticles

@Configuration
public class MainConfig {

    @Autowired
    ProductVerticle productVerticle;

    @Autowired
    zjc.examples.vertx.springBoot.verticle.MQHandleVerticle MQHandleVerticle;

    @PostConstruct
    public void deployVerticle() {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(productVerticle);
        vertx.deployVerticle(MQHandleVerticle);
    }
}

Verticles

ProductVerticle.java

@Component
public class ProductVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> startPromise) throws Exception {
        vertx.createHttpServer(new HttpServerOptions())
                .requestHandler(getRouter())
                .listen(8080,asyncResult -> {
                    if (asyncResult.succeeded()) {
                        System.out.println("SUCCESS");
                        startPromise.complete();
                    } else {
                        System.out.println("FAILED");
                        startPromise.fail(asyncResult.cause());
                    }
                });
    }

    private Router getRouter() throws FileNotFoundException {
        Router router = Router.router(vertx);

        // call API and place message in MQ after completion
        router.post("/product").handler(this::createProductRecAndplaceMessageInMq);

        // GET request
        router.get("/product").handler(handler -> handler.response().end("all Product details "));

        // GET request with path parameters
        router.get("/product/:name").handler(handler -> {
            String name = handler.pathParam("name");
            handler.response().end(String.format("Product %s details ", name));
        });

        // Get reroute
        router.get("/producer").handler(a -> {
            a.reroute("/subproducer");
        });
        router.get("/subproducer").handler( i-> i.response().end("Hello subproducer"));

        // POST request
        router.post("/mail").handler(context -> {
            context.request().bodyHandler(System.out::println);
            context.response().end("done");
        });

        return router;
    }

    private void createProductRecAndplaceMessageInMq(RoutingContext ctx) {
        //  perform API call task...
        vertx.eventBus().request("mqHandlerOnAPICompletion","", reply -> ctx.request().response().end((String) "Created product record. Placing message in MQ: " + reply.result().body()));
    }

MQHandleVerticle.java

@Component
public class MQHandleVerticle extends AbstractVerticle {

    @Override
    public void start() {
        vertx.eventBus().consumer("mqHandlerOnAPICompletion", msg -> {
            System.out.println("placing message in MQ");

            // place a success message in MQ with required details
            msg.reply("Success");
        });
    }
}

Running

mvn spring-boot:run

Some results

Source Code: https://github.com/ZbCiok/zjc-examples/tree/main/vertx/springBoot/vertx-integrate-spring-boot

Mark