1

This the class that runs verticles :

public class RequestResponseExample {
    public static void main(String[] args) throws InterruptedException {
        Logger LOG = LoggerFactory.getLogger(RequestResponseExample.class);

        final Vertx vertx = Vertx.vertx();
        final Handler<AsyncResult<String>> RequestHandler = dar-> vertx.deployVerticle(new ResponseVerticle());

        vertx.deployVerticle(new ResponseVerticle(), RequestHandler);
    }
}

This is The Request verticle Class :

public class RequestVerticle extends AbstractVerticle{
    public static final Logger LOG = LoggerFactory.getLogger(RequestVerticle.class);
    static final String ADDRESS = "my.request.address";

    @Override
    public void start() throws Exception {
       
        Router router = Router.router(vertx);
        router.get("/Test").handler(rc -> rc.response().sendFile("index.html"));

        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }
}

This is The Response verticle Class: Here Im having a difficulty getting The Inserted value in the HTML file

public class ResponseVerticle extends AbstractVerticle{
    public static final Logger LOG = LoggerFactory.getLogger(RequestVerticle.class);
    @Override
    public void start() throws Exception {

    Router router = Router.router(vertx);

    // How to handle the POST value ?
    router.post("/Test/Result").handler(rc -> rc.end("The Post Value"));

    vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
}
  • 1
    I am not sure what you are trying to achieve, but be aware that you deploy ResponseVerticle twice. You dont use RequestVerticle. And you are missing BodyHandler on the route. – taygetos Apr 23 '22 at 13:42

1 Answers1

1

When the user invokes POST /Test/Result and sends some POST value, you receive it in your Response verticle class (third snippet). If you want to share that method with other verticles, you should store it somewhere inside the handler method so other verticles access it or immediately forward it to other verticle via the event bus.

One possible solution would be to create a third verticle (e.g. StorageVerticle) which has get and set methods. That way, ResponseVerticle invokes the set method to store the value it got, and the RequestVerticle invokes the get method to fetch the value the user sent on calling POST method.

The other solution of direct communication between verticles involves the Event Bus message exchange - one verticle publishes/sends a message and all other verticles can register as a consumer to get that message. More on this you can find here: https://vertx.io/docs/vertx-core/java/#_the_event_bus_api.

It is hard to say which approach is better because it is case-to-case basis and I have limited information here about the scope of the project.

ph0enix
  • 763
  • 2
  • 8
  • 23