子流程支援
if…else
和 publish-subscribe
元件中的某些元件提供使用子流程指定其邏輯或對應的功能。最簡單的範例是 .publishSubscribeChannel()
,如下列範例所示
@Bean
public IntegrationFlow subscribersFlow() {
return flow -> flow
.publishSubscribeChannel(Executors.newCachedThreadPool(), s -> s
.subscribe(f -> f
.<Integer>handle((p, h) -> p / 2)
.channel(c -> c.queue("subscriber1Results")))
.subscribe(f -> f
.<Integer>handle((p, h) -> p * 2)
.channel(c -> c.queue("subscriber2Results"))))
.<Integer>handle((p, h) -> p * 3)
.channel(c -> c.queue("subscriber3Results"));
}
您可以使用個別的 IntegrationFlow
@Bean
定義來達成相同的結果,但我們希望您會發現子流程樣式的邏輯組合很有用。我們發現這樣可以產生更簡短(因此更易於閱讀)的程式碼。
從 5.3 版開始,提供基於 BroadcastCapableChannel
的 publishSubscribeChannel()
實作,以在經紀商支援的訊息通道上設定子流程訂閱者。例如,我們現在可以在 Jms.publishSubscribeChannel()
上將多個訂閱者設定為子流程
@Bean
public JmsPublishSubscribeMessageChannelSpec jmsPublishSubscribeChannel() {
return Jms.publishSubscribeChannel(jmsConnectionFactory())
.destination("pubsub");
}
@Bean
public IntegrationFlow pubSubFlow(BroadcastCapableChannel jmsPublishSubscribeChannel) {
return f -> f
.publishSubscribeChannel(jmsPublishSubscribeChannel,
pubsub -> pubsub
.subscribe(subFlow -> subFlow
.channel(c -> c.queue("jmsPubSubBridgeChannel1")))
.subscribe(subFlow -> subFlow
.channel(c -> c.queue("jmsPubSubBridgeChannel2"))));
}
類似的 publish-subscribe
子流程組合提供了 .routeToRecipients()
方法。
另一個範例是在 .filter()
方法上使用 .discardFlow()
而不是 .discardChannel()
。
.route()
值得特別關注。請考慮下列範例
@Bean
public IntegrationFlow routeFlow() {
return f -> f
.<Integer, Boolean>route(p -> p % 2 == 0,
m -> m.channelMapping("true", "evenChannel")
.subFlowMapping("false", sf ->
sf.<Integer>handle((p, h) -> p * 3)))
.transform(Object::toString)
.channel(c -> c.queue("oddChannel"));
}
.channelMapping()
繼續像在常規 Router
對應中一樣運作,但 .subFlowMapping()
將該子流程繫結到主流程。換句話說,任何路由器的子流程都會在 .route()
之後返回主流程。
有時,您需要從
Caused by: org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.router.MethodInvokingRouter@7965a51c) is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow. 當您將子流程設定為 lambda 時,框架會處理與子流程的請求-回覆互動,而不需要閘道。 |
子流程可以巢狀到任何深度,但我們不建議這樣做。實際上,即使在路由器案例中,在流程中新增複雜的子流程也會很快開始看起來像一盤義大利麵,並且難以讓人解析。
在 DSL 支援子流程設定的情況下,當通常需要通道來設定元件,並且該子流程以
框架會在內部建立 |