集成流组合
使用MessageChannel
抽象作为 Spring Integration 中的一等公民,集成流的组成总是被假定的。
流中任何终端节点的输入通道都可用于从任何其他终端节点发送消息,而不仅仅是从将此通道作为输出的终端节点发送消息。
此外,使用@MessagingGateway
合约、内容扩充器组件、复合端点(如<chain>
,现在有了IntegrationFlow
Beans(例如IntegrationFlowAdapter
),在较短的、可重用的部分之间分配业务逻辑非常简单。
最终组合所需要的只是关于MessageChannel
以发送到或接收自。
从 version 开始5.5.4
,从MessageChannel
并对最终用户隐藏实现详细信息,IntegrationFlow
介绍from(IntegrationFlow)
factory 方法允许启动电流IntegrationFlow
从现有流的输出中:
@Bean
IntegrationFlow templateSourceFlow() {
return IntegrationFlow.fromSupplier(() -> "test data")
.channel("sourceChannel")
.get();
}
@Bean
IntegrationFlow compositionMainFlow(IntegrationFlow templateSourceFlow) {
return IntegrationFlow.from(templateSourceFlow)
.<String, String>transform(String::toUpperCase)
.channel(c -> c.queue("compositionMainFlowResult"))
.get();
}
另一方面,IntegrationFlowDefinition
已添加to(IntegrationFlow)
terminal 运算符在一些其他流的输入通道上继续电流:
@Bean
IntegrationFlow mainFlow(IntegrationFlow otherFlow) {
return f -> f
.<String, String>transform(String::toUpperCase)
.to(otherFlow);
}
@Bean
IntegrationFlow otherFlow() {
return f -> f
.<String, String>transform(p -> p + " from other flow")
.channel(c -> c.queue("otherFlowResultChannel"));
}
流中间的组合可以通过现有的gateway(IntegrationFlow)
EIP 方法。
通过这种方式,我们可以从更简单、可重用的逻辑块组合流,从而构建任何复杂的流。
例如,您可以添加IntegrationFlow
beans 作为依赖项,只需将它们的配置类导入到最终项目中并为您的IntegrationFlow
定义。