反应式消息传递 - ClassCastException 无法转换为 class io.smallrye.mutiny.Multi

reactive messaging - ClassCastException cannot be cast to class io.smallrye.mutiny.Multi

@Incoming("from-processor-to-consumer")
public Multi<Void> consume(Multi<String> stream) {
    return stream.onItem()
            .invoke(msg -> {
                log.infof("consumer received %s", msg);
            })
            .onItem()
            .ignore();
}

当 运行 此方法引发 ClassCastException:

consume has thrown an exception: java.lang.ClassCastException: class java.lang.String cannot be cast to class io.smallrye.mutiny.Multi

我可以食用 Multi<String> 吗?

SmallRye Reactive Messaging > Development Model > Consuming Payloads好像一次只能消费一条消息

首先,我必须承认错误信息是无用的,应该改进。

问题出在您的应用程序签名上。

@Incoming("from-processor-to-consumer")
@Outgoing("this-is-missing-in-your-code")
public Multi<Void> consume(Multi<String> stream) {
    return stream.onItem()
            .invoke(msg -> {
                log.infof("consumer received %s", msg);
            })
            .onItem()
            .ignore();
}

缺少 @Outgoing 注释。返回 Multi, 时,您需要说“where.”

如果你的方法是终端(意思是最终消费者),你不能注入一个Multi。解决方法是使用简单的 no-op 方法:

@Incoming("from-processor-to-consumer")
@Outgoing("this-is-missing-in-your-code")
public Multi<String> consume(Multi<String> stream) {
    return stream.onItem()
            .invoke(msg -> {
                log.infof("consumer received %s", msg);
            });
}


@Incoming("this-is-missing-in-your-code")
void noop(String s) { }