整合流組合
由於 `MessageChannel` 抽象是 Spring Integration 中的一級公民,整合流的組合一直被認為是理所當然的。流中任何端點的輸入通道都可以用來從任何其他端點發送訊息,而不僅僅是那些將此通道作為輸出的端點。此外,藉助 `@MessagingGateway` 合約、內容增強器(Content Enricher)元件、像 `
從 `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` 定義。