此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
子流支持
一些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()
方法。
另一个例子是使用.discardFlow()
而不是.discardChannel()
在.filter()
方法。
这.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 支持子流配置的情况下,当正在配置的组件通常需要通道时,并且该子流以
框架在内部创建了一个 |