子流支援

某些 if…​elsepublish-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 版本開始,提供了基於 BroadcastCapableChannelpublishSubscribeChannel() 實現,用於在代理支援的訊息通道上配置子流訂閱者。例如,我們現在可以在 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() 之後都會返回到主流程。

有時,你需要從 .subFlowMapping() 中引用一個現有的 IntegrationFlow @Bean。以下示例展示瞭如何實現

@Bean
public IntegrationFlow splitRouteAggregate() {
    return f -> f
            .split()
            .<Integer, Boolean>route(o -> o % 2 == 0,
                    m -> m
                            .subFlowMapping(true, oddFlow())
                            .subFlowMapping(false, sf -> sf.gateway(evenFlow())))
            .aggregate();
}

@Bean
public IntegrationFlow oddFlow() {
    return f -> f.handle(m -> System.out.println("odd"));
}

@Bean
public IntegrationFlow evenFlow() {
    return f -> f.handle((p, h) -> "even");
}


在這種情況下,當你需要從這樣的子流接收回復並繼續主流程時,此 IntegrationFlow bean 引用(或其輸入通道)必須用 .gateway() 封裝,如上例所示。上例中的 oddFlow() 引用未封裝到 .gateway() 中。因此,我們不期望從該路由分支獲得回覆。否則,你將遇到類似於以下內容的異常

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 支援子流配置的情況下,當通常需要通道用於正在配置的元件時,並且該子流以 channel() 元素開頭時,框架會在元件輸出通道和流程輸入通道之間隱式放置一個 bridge()。例如,在這個 filter 定義中

.filter(p -> p instanceof String, e -> e
	.discardFlow(df -> df
                         .channel(MessageChannels.queue())
                         ...)

框架在內部建立一個 DirectChannel bean 以注入到 MessageFilter.discardChannel 中。然後,它將子流封裝到一個以這個隱式通道開頭的 IntegrationFlow 中進行訂閱,並在流程中指定的 channel() 之前放置一個 bridge。當使用現有的 IntegrationFlow bean 作為子流引用(而不是內聯子流,例如 lambda)時,不需要這樣的橋接,因為框架可以從流程 bean 中解析第一個通道。使用內聯子流時,輸入通道尚不可用。

© . This site is unofficial and not affiliated with VMware.