本文介绍了如何使用 vertx.eventBus().send() 方法将 RoutingContext 对象从路由 Verticle 发送到其他 Verticle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从具有路由 URL 的路由顶点,我想将 RoutingContext 对象发送到另一个顶点.我相信我们只能使用 vertx.eventBus().send() 将消息从路由顶点发送到其他动作顶点.我可以将 RoutingContext 对象作为消息发送吗?

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?

在路由器垂直我正在做vertx.eventBus().send("address", routingContext)

在消费者verticle中我正在做vertx.eventBus().consumer("address").handler(message -> {RoutingContext routingContext = message.body();LOGGER.info("routingContext body = "+routingContext.getBodyAsString());});

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

但看起来vertx本身无法执行'vertx.eventBus().send'谁能告诉我如何使用 vertx.eventBus().send 方法发送 RoutingContext 对象?

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?

推荐答案

如果你想通过 Vert.x EventBus 发送一个对象,它不是 JsonObject 或像 String 这样的普通 Java 对象,例如,您需要实现自己的编解码器.

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

在我看来,为 RoutingContext 实现它仍然没有多大意义.只传输您实际需要的部分,而不是整个对象.

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.

这篇关于如何使用 vertx.eventBus().send() 方法将 RoutingContext 对象从路由 Verticle 发送到其他 Verticle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 14:30