整合流組合
由於 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 定義中即可。