Spring 具有多个规则的集成路由
Spring integration routing with multiple rules
我正在使用 Spring 与 DSL 的集成,我需要为各种渠道路由消息。简而言之,如果失败,他们应该转到输出通道,如果成功,他们应该转到两个通道之一。两种路由均基于 header 参数。
我创建了两个路由器。一个处理失败,另一个处理成功,但是当我尝试启动应用程序时,出现以下错误:
nested exception is org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.router.MethodInvokingRouter@50ac1249) is a one-way 'MessageHandler' and it isn't appropriate to configure 'failure-channel'. This is the end of the integration flow.
我的流程定义
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows.from("from")
.route(Message.class,
message -> message
.getHeaders().containsKey("FAILURE"),
mapping -> mapping
.channelMapping(true, "failure-channel"))
.route(Message.class,
message -> message
.getHeaders().get("NEXT"),
mapping -> mapping
.channelMapping("first", "first-channel")
.channelMapping("second", "second-channel")
.get();
}
我该如何实现这个逻辑?据我在文档中读到的那样,定义多个路由没有问题,并且两个条件都是独立有效的,因为为了让它工作,我创建了另一个接收成功的通道,并且只执行第二个路由。
我假设问题是因为第一个路由器使用了消息,但我正在寻找类似于 if route A does not resolve go to route B 的行为。
我使用 subFlow()
找到了答案。在这种情况下,我最终得到:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows.from("from")
.route(Message.class,
message -> message
.getHeaders().containsKey("FAILURE"),
mapping -> mapping
.channelMapping(true, "failure-channel")
.subFlowMapping(false, sf -> sf.route(Message.class,
message -> message
.getHeaders().get("NEXT"),
subMapping -> subMapping
.channelMapping("first", "first-channel")
.channelMapping("second", "second-channel")
.resolutionRequired(true))
.get();
}
参见RouterSpec
:
/**
* Make a default output mapping of the router to the parent flow.
* Use the next, after router, parent flow {@link MessageChannel} as a
* {@link AbstractMessageRouter#setDefaultOutputChannel(MessageChannel)} of this router.
* @return the router spec.
*/
public S defaultOutputToParentFlow() {
文档中也有注释:https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-routers
The .defaultOutputToParentFlow()
of the .routeToRecipients()
definition lets you set the router’s defaultOutput
as a gateway to continue a process for the unmatched messages in the main flow.
因此,您的第一个没有 FAILURE
header 的路由器会将其输出发送到所需 NEXT
路由器的主流。
我正在使用 Spring 与 DSL 的集成,我需要为各种渠道路由消息。简而言之,如果失败,他们应该转到输出通道,如果成功,他们应该转到两个通道之一。两种路由均基于 header 参数。 我创建了两个路由器。一个处理失败,另一个处理成功,但是当我尝试启动应用程序时,出现以下错误:
nested exception is org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.router.MethodInvokingRouter@50ac1249) is a one-way 'MessageHandler' and it isn't appropriate to configure 'failure-channel'. This is the end of the integration flow.
我的流程定义
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows.from("from")
.route(Message.class,
message -> message
.getHeaders().containsKey("FAILURE"),
mapping -> mapping
.channelMapping(true, "failure-channel"))
.route(Message.class,
message -> message
.getHeaders().get("NEXT"),
mapping -> mapping
.channelMapping("first", "first-channel")
.channelMapping("second", "second-channel")
.get();
}
我该如何实现这个逻辑?据我在文档中读到的那样,定义多个路由没有问题,并且两个条件都是独立有效的,因为为了让它工作,我创建了另一个接收成功的通道,并且只执行第二个路由。
我假设问题是因为第一个路由器使用了消息,但我正在寻找类似于 if route A does not resolve go to route B 的行为。
我使用 subFlow()
找到了答案。在这种情况下,我最终得到:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlows.from("from")
.route(Message.class,
message -> message
.getHeaders().containsKey("FAILURE"),
mapping -> mapping
.channelMapping(true, "failure-channel")
.subFlowMapping(false, sf -> sf.route(Message.class,
message -> message
.getHeaders().get("NEXT"),
subMapping -> subMapping
.channelMapping("first", "first-channel")
.channelMapping("second", "second-channel")
.resolutionRequired(true))
.get();
}
参见RouterSpec
:
/**
* Make a default output mapping of the router to the parent flow.
* Use the next, after router, parent flow {@link MessageChannel} as a
* {@link AbstractMessageRouter#setDefaultOutputChannel(MessageChannel)} of this router.
* @return the router spec.
*/
public S defaultOutputToParentFlow() {
文档中也有注释:https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-routers
The
.defaultOutputToParentFlow()
of the.routeToRecipients()
definition lets you set the router’sdefaultOutput
as a gateway to continue a process for the unmatched messages in the main flow.
因此,您的第一个没有 FAILURE
header 的路由器会将其输出发送到所需 NEXT
路由器的主流。