Java - Vertx - 发布-订阅模式:在自己的消费者中发布消息。这是一个坏主意吗?

Java - Vertx - Publish–Subscribe pattern : publishing message inside its own consumer. Is this a bad idea?

我是发布-订阅模式的新手,我们在应用程序中使用 Vertx。

我正在尝试针对某些用例执行此操作,我在其自己的消费者中发布:

private void functionality() {
    EventBus eb = Vertx.currentContext().owner().eventBus();
    MessageConsumer<String> consumer = eb.consumer("myAddress");
    consumer.handler(message -> {
        if (condition1) {
            doOperationWhichMightChangeTheCondition();
            eb.publish("myAddress","Start Operation");
        }else {
            log.info("Operations on all assets completed");
        }
    });
    eb.publish("myAddress","Start Operation");
}

这是个坏主意吗?这是否也会导致递归调用等 Whosebug 错误或任何其他问题?

EventBus.publish方法是异步的;它不会阻塞等待消费者 receive/process 发布的消息。因此,在消费者内部调用 publish 是完全安全的,消费者随后将消费发布的消息。在内部,Vert.x 让 Netty 安排对消费者的另一个调用,直到当前调用(以及在 Netty 事件循环中提前安排的任何其他方法)完成后才会 运行。您可以通过编写一个测试来轻松地向自己证明这一点,该测试使用一个发布到它正在使用的地址的消费者。您不会看到 WhosebugError.