0

From my routing verticle which has route URL, I want to send RoutingContext object to another verticle. I believe we can only use vertx.eventBus().send() to send message from routing verticle to some other action verticle. Can I send RoutingContext object as a message?

In router verticle I am doing vertx.eventBus().<RoutingContext>send("address", routingContext)

and in consumer verticle I am doing vertx.eventBus().<RoutingContext>consumer("address").handler(message -> { RoutingContext routingContext = message.body(); LOGGER.info("routingContext body = "+routingContext.getBodyAsString()); });

but looks like vertx itself is not able to execute 'vertx.eventBus().send' Could anyone please let me know how could I send RoutingContext object using vertx.eventBus().send method?

Nags
  • 273
  • 1
  • 4
  • 16
  • generally speaking, event buses are designed for distributing data (the Vert.x. implementation being no exception). they are not designed for distributing object instances like a `RoutingContext` instance. – homerman Aug 16 '19 at 05:21

1 Answers1

2

If you want to send an object over Vert.x EventBus which is not a JsonObject or a plain Java object like a String, for example, you need to implement your own codec.

What that means is basically describing which parts of the object you want to transfer.

You can see examples of implementing your custom codec here:

https://github.com/vert-x3/vertx-examples/blob/master/core-examples/src/main/java/io/vertx/example/core/eventbus/messagecodec/util/CustomMessageCodec.java

It still doesn't make much sense to implement it for RoutingContext, in my opinion. Just transfer the parts that you actually need, not the entire object.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • I actually need HttpServerResponse which we get using routingContext.response(). I need it to use in pipeTo method to stream a zip file from file server to response of incoming request. If I don't pass RoutingContext or HttpServerResponse from router verticle to another verticle, then I cannot make use of pipeTo in that another verticle. This has put a limitation on me to make use of pipeTo in router verticle itself. This is creating problem when I am trying create multiple instances of router verticle using DeploymentOptions().setInstances() method. – Nags Sep 05 '19 at 22:57
  • Always pipe from your router. Instead of sending entire `RoutingContext` over `EventBus`, send only what you need, and as a reply message return a `Buffer`, which you can later stream to a `pipe`. – Alexey Soshin Sep 06 '19 at 09:36