How can I send RoutingContext object from routing verticle to some other verticle using vertx.eventBus().send() method?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 14:51:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/57513150/how-can-i-send-routingcontext-object-from-routing-verticle-to-some-other-verticl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!