整合流程組成
在 Spring Integration 中,MessageChannel
抽象化作為一級公民,整合流程的組成一直被認為是理所當然的。流程中任何端點的輸入通道都可以用來傳送來自任何其他端點的訊息,而不僅僅是來自將此通道作為輸出的端點。此外,透過 @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
定義自動裝配即可。