多规则的Spring集成路由

问题描述 投票:1回答:2

我正在使用Spring Integration与DSL,我需要为不同的通道路由消息。简单地说,在失败的情况下,他们应该去一个输出通道,在成功的情况下,他们应该去两个通道之一。这两种路由都是基于头参数的.我创建了两个路由器。我创建了两个路由器,一个用于处理失败,另一个用于处理成功,但当我尝试启动应用程序时,我得到以下错误。

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。.

java spring-boot spring-integration spring-integration-dsl
2个回答
1
投票

查看 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.iospring-integrationdocscurrentreferencehtmldsl.html#java-dsl-routers。

.defaultOutputToParentFlow().routeToRecipients() 定义可以让你设置路由器的 defaultOutput 作为网关,继续处理主流中未匹配的消息。

所以,你的第一个路由器没有一个 FAILURE 头将把它的输出发送到主流,以获得所需的信息。NEXT 路由器。


0
投票

我通过使用 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();
}
© www.soinside.com 2019 - 2024. All rights reserved.