|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
集成流组合
随着 MessageChannel 抽象在 Spring Integration 中成为一等公民,集成流的组合始终被假定是可行的。
流中任何端点的输入通道都可以用于从任何其他端点发送消息,而不仅限于以该通道为输出的端点。
此外,借助 @MessagingGateway 契约、内容增强器组件、复合端点(如 <chain>),以及现在的 IntegrationFlow Bean(例如 IntegrationFlowAdapter),将业务逻辑分布在更短、可重用的部分变得非常简单。
最终组合所需的一切,就是了解要发送或接收的 MessageChannel。
从版本 5.5.4 开始,为了更抽象地处理 MessageChannel 并向最终用户隐藏实现细节,IntegrationFlow 引入了 from(IntegrationFlow) 工厂方法,允许从现有流的输出启动当前的 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) 终端操作符,以便在某个其他流的输入通道处继续当前流:
@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 Bean 的库作为依赖项,只需将这些配置类导入最终项目,并自动注入到您的 IntegrationFlow 定义中即可。