问题
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