eventBus().consumer() 永远不会被调用

eventBus().consumer() never gets called

我已将 vert.x 添加到 Spring 引导应用程序,方法是将这些依赖项添加到 build.gradle:

    compile "io.vertx:vertx-core:3.8.1"
    compile "io.vertx:vertx-lang-groovy:3.8.1"

我想使用 vert.x EventBus 在单个 JVM 应用程序(无 Verticles)中实现响应式代码。

我已验证出站拦截器有效并且 SharedData 有效。但是,没有迹象表明入站拦截器或消费者被调用过。

我怀疑我在配置 vert.x 时忽略了一些东西,或者在 Spring 中嵌入 vert.x 引导以某种方式阻止接收入站消息。

        Vertx vertx = Vertx.vertx();

        vertx.eventBus().addInboundInterceptor(msg -> {
            log.debug("abc inbound "+msg);
        });

        vertx.eventBus().addOutboundInterceptor(msg -> {
            log.debug("abc outbound "+msg);
        });

        vertx.eventBus().<String>consumer("abc", (Message<String> msg) -> {
            log.debug("abc handler");
        });

        vertx.eventBus().<String>localConsumer("localabc", (Message<String> msg) -> {
            log.debug("local abc handler");
        });

        vertx.eventBus().consumer("abc", msg -> {
            log.debug("abc handler 2");
        });

        vertx.eventBus().localConsumer("localabc", msg -> {
            log.debug("local abc handler 2");
        });

        MessageConsumer<String> consumer1 = vertx.eventBus().consumer("abc");
        consumer1.handler(msg -> {
            log.debug("abc handler 3");
        });

        MessageConsumer<String> consumer2 = vertx.eventBus().localConsumer("localabc");
        consumer2.handler(msg -> {
            log.debug("local abc handler 3");
        });

        LocalMap<String, String> localMap = vertx.sharedData().getLocalMap("abc");
        localMap.put("abc", "abc map");

        vertx.eventBus().publish("abc", "test", new DeliveryOptions().setLocalOnly(true));
        vertx.eventBus().publish("localabc", "localtest", new DeliveryOptions().setLocalOnly(true));
        //LocalMap<String, String> localMap = vertx.sharedData().getLocalMap("abc");
        log.debug("abc map contains "+localMap.get("abc"));

这是输出。没有任何类型的错误。

abc outbound io.vertx.core.eventbus.impl.EventBusImpl$OutboundDeliveryContext@3bab9a17
abc outbound io.vertx.core.eventbus.impl.EventBusImpl$OutboundDeliveryContext@54887f7e
abc map contains abc map

您正在使用 outboundInterceptor 而没有 next()

所以它就像一个过滤器。它会捕获您的所有消息,并且永远不会将它们转发给消费者。

您可以只使用:

vertx.eventBus().addOutboundInterceptor(msg -> {
    log.debug("abc outbound "+msg);
    msg.next();
});