|
对于最新稳定版本,请使用 Spring Integration 7.0.0! |
子流支持
一些如果。。。还和发布订阅组件通过使用子流来指定其逻辑或映射的能力。
最简单的样本为.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"));
}
你也可以用分开达到同样的结果集成流程 @Bean定义,但我们希望你觉得子流式逻辑组合对你有用。
我们发现,这会让代码更短(因此更易读)。
从5.3版本开始,a广播能力频道-基于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"))));
}
类似的发布订阅子流组成提供.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()正常运行路由器映射,但.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支持子流配置的情况下,通常需要一个通道用于配置的组件,而该子流以
框架内部创建了 |