|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
子流程支持
一些 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 支持子流配置的情况下,当为正在配置的组件通常需要通道,且该子流以
框架内部会创建一个 |