Java 多规则Spring集成路由
我正在使用Spring与DSL的集成,我需要为各种渠道路由消息。简单地说,如果失败,他们应该去一个输出通道,如果成功,他们应该去两个通道之一。两种路由都基于报头参数。 我创建了两个路由器。一个用来处理失败,另一个用来处理成功,但是当我尝试启动应用程序时,我得到以下错误:Java 多规则Spring集成路由,java,spring-boot,spring-integration,spring-integration-dsl,Java,Spring Boot,Spring Integration,Spring Integration Dsl,我正在使用Spring与DSL的集成,我需要为各种渠道路由消息。简单地说,如果失败,他们应该去一个输出通道,如果成功,他们应该去两个通道之一。两种路由都基于报头参数。 我创建了两个路由器。一个用来处理失败,另一个用来处理成功,但是当我尝试启动应用程序时,我得到以下错误: nested exception is org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springfra
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();
}
我如何实现这个逻辑?据我在文档中所读到的,定义多个路由没有问题,两个条件单独有效,因为为了让它工作,我创建了另一个接收成功的通道,并且只执行第二个路由
我假设问题是因为第一个路由器使用消息,但我正在寻找类似于如果路由a不解析转到路由B的行为。我通过使用
子流()找到了答案。
。在这种情况下,我的结论是:
@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();
}
请参阅路由规范:
/**
* 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() {
文件中还有一个注释:
.routeToRecipients()
定义的.DefaultOutputOparentFlow()
允许您将路由器的defaultOutput
设置为网关,以继续处理主流中不匹配的消息
因此,没有
故障
头的第一个路由器将把它的输出发送到所需下一个
路由器的主流。所以逻辑是:默认故障还是其他,对吗?我首先想到了这种方法,因为我认为使用默认输出是一个非常好的主意,但后来我发现了子映射。尽管我不确定这是否是最好的解决方案,但关键是不要过度滥用子流,否则它就是lambda spaghettiYeh。我可以清楚地看出原因。那我就试着改变一下,看看情况如何。泰