Grammin Grammin - 1 year ago 87
Java Question

What messaging pattern should I use to process and return responses from a rest request?

I have a hub and spoke architecture similar to this:

enter image description here

where a GET request comes into the hub and it routes it to one of the spokes for processing. On the hub I also put the request in a map with a UUID so that I can return the proper response when I get the data back from processing. The spokes are identical and are used to balance the load. I then need to pass the information back to the hub from the spoke and return the proper reponse.

I would like to do the messaging using JMS.

What is the best combination of integration patterns to accomplish this?

Answer Source

You already have Request/Reply within Vert.x, so you can achieve this behavior with about 20 lines of code:

public static void main(String[] args) {

    Vertx vertx = Vertx.vertx();

    Router router = Router.router(vertx);

    router.get("/").handler((request) -> {
        // When hub receives request, it dispatches it to one of the Spokes
        String requestUUID = UUID.randomUUID().toString();
        vertx.eventBus().send("processMessage", requestUUID, (spokeResponse) -> {
            if (spokeResponse.succeeded()) {
                request.response().end("Request " + requestUUID + ":" + spokeResponse.result().body().toString());
            // Handle errors

    // We create two Spokes
    vertx.deployVerticle(new SpokeVerticle());
    vertx.deployVerticle(new SpokeVerticle());

    // This is your Hub

And here's what Spoke looks like:

 * Static only for the sake of example
static class SpokeVerticle extends AbstractVerticle {

    private String id;

    public void start() { = UUID.randomUUID().toString();

        vertx.eventBus().consumer("processMessage", (request) -> {
            // Do something smart

            // Reply
            request.reply("I'm Spoke " + id + " and my reply is 42");

Try accessing it in your browser on http://localhost:8888/

You should see that request ID is generated every time, while only one of two Spokes answers your request.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download