Spring 集成不能使用多个出站通道适配器

Spring Integration can’t use multiple Outbound Channel Adapters

我只想在先前的通道适配器写入成功后写入通道适配器。我正在尝试通过以下方式做到这一点:

@Bean
public IntegrationFlow buildFlow() {
    return IntegrationFlows.from(someChannelAdapter)
         .handle(outboundChannelAdapter1)
         .handle(outboundChannelAdapter2)
         .get();
}

但我收到以下异常:The ‘currentComponent’ (…ReactiveMessageHandlerAdapter) is a one-way 'MessageHandler’ and it isn’t appropriate to configure ‘outputChannel’. This is the end of the integration flow.

我该如何执行此操作?

如果您的处理程序实现是 one-way、fire-n-forget,那么确实没有理由继续流程。如果当前处理程序是 reply-producing,它可以继续进行配置,并且我们可以构建一条消息发送到下一个通道。

在您的情况下 .handle(outboundChannelAdapter1) 只是 void,因此下一个 .handle(outboundChannelAdapter2) 不会有任何继续流程。所以,框架会提示你这样的配置是错误的。它被称为流是有原因的:当前端点的结果将成为下一个端点的输入。如果没有结果,就没有继续。在您看来,它还能如何发挥作用?

重点是需要向您的通道适配器写入一些内容。解决方案之一是 PublishSubscribeChannel,它向所有订阅者分发相同的输入消息。如果这符合您的期望,那么请查看它在 Java DSL 中的支持:https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-subflows.

另一种方式是 RecipientListRouter 模式:https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#router-implementations-recipientlistrouter.

您也可以使用 WireTap 实现相同的效果,但这取决于您解决方案的业务逻辑:https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-wiretap.

但无论如何:您需要了解只有在其通道有输入消息时才能调用第二个处理程序。在所有这些情况下,我向您展示了它与您发送给第一个处理程序的消息完全相同。如果您的期望不同,请详细说明如果第一个处理程序没有 return 任何内容,您希望第二个处理程序收到什么样的消息。