UNI等待VERTX EventBus消息
我有两个端点:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/waitForEvent")
public Uni<Object> waitForEvent() {
return Uni.createFrom().emitter(em -> {
//wait for event from eventBus
// eventBus.consumer("test", msg -> {
// System.out.printf("receive event: %s\n", msg.body());
// em.complete(msg);
// });
}).ifNoItem().after(Duration.ofSeconds(5)).failWith(new RuntimeException("timeout"));
}
@GET
@Path("/send")
public void test() {
System.out.println("send event");
eventBus.send("test", "send test event");
}
wareforevent()仅在从EventBus接收事件的情况下才能完成。如何使用Vertx和Mutiny实现这一目标?
I have two endpoints:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/waitForEvent")
public Uni<Object> waitForEvent() {
return Uni.createFrom().emitter(em -> {
//wait for event from eventBus
// eventBus.consumer("test", msg -> {
// System.out.printf("receive event: %s\n", msg.body());
// em.complete(msg);
// });
}).ifNoItem().after(Duration.ofSeconds(5)).failWith(new RuntimeException("timeout"));
}
@GET
@Path("/send")
public void test() {
System.out.println("send event");
eventBus.send("test", "send test event");
}
The waitForEvent() should only complete if it receives the event from the eventBus. How can I achieve this using vertx and mutiny?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,我们避免使用这种模式,并使用事件总线中的请求/回复机制:
使用两个端点实现(如问题)时,它可能会变得更加复杂,好像您对
/wateforevent 端点,您需要确保每个“消费者”收到消息。
仍然有可能,但是将需要这样的东西:
请确保使用
io.vertx.mutiny.core.eventbus.eventbus.eventbus
事件总线的变体。In general, we avoid that kind of pattern and use the request/reply mechanism from the event bus:
When implementing with two endpoints (as in the question), it can become a bit more complicated as if you have multiple calls to the
/waitForEvent
endpoint, you need to be sure that every "consumer" get the message.It is still possible, but would will need something like this:
Be sure to use the
io.vertx.mutiny.core.eventbus.EventBus
variant of the event bus.