此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
集成流组合
使用MessageChannel
抽象作为 Spring Integration 中的一等公民,集成流的组成总是被假设的。
流中任何端点的输入通道可用于从任何其他端点发送消息,而不仅仅是从具有此通道作为输出的端点发送消息。
此外,使用@MessagingGateway
contract、Content Enricher 组件、复合端点(如<chain>
,现在使用IntegrationFlow
Beans(例如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
定义。