Spring Integration:基于内容的路由器,默认输出通道?

时间:2011-07-27 14:20:19

标签: java spring routing spring-integration

如果表达式值与任何映射都不匹配,我想使用Spring Integration来实现使用默认输出通道的基于内容的路由器。这是我的bean定义:

<int:router input-channel="channel_in" default-output-channel="channel_default" expression="payload.name">
    <int:mapping value="foo" channel="channel_one" />
    <int:mapping value="bar" channel="channel_two" />

但是,似乎从不使用默认输出通道。如果表达式评估为例如'baz',路由器似乎正在寻找一个名为'baz'的频道,而不是路由到'channel_default'频道:

org.springframework.integration.MessagingException: failed to resolve channel name 'baz'
  Caused by: org.springframework.integration.support.channel.ChannelResolutionException: 
    failed to look up MessageChannel bean with name 'baz'
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No bean named 'baz' is defined

使用XML命名空间,我想要的是什么,或者我是否需要编写自己的实现代码?

3 个答案:

答案 0 :(得分:10)

事实证明,我必须做的就是将路由器的ignore-channel-name-resolution-failures属性设置为false:

<int:router input-channel="channel_in" default-output-channel="channel_default" 
  expression="payload.name" ignore-channel-name-resolution-failures="true">
    <int:mapping value="foo" channel="channel_one" />
    <int:mapping value="bar" channel="channel_two" />
</int:router>

我以为我之前曾尝试过,但我似乎没有。

答案 1 :(得分:0)

如果用于Spring Boot 2.1.2.RELEASE

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>

然后需要 resolution-required =“ false” ,而不是ignore-channel-name-resolution-failures。

<int:router input-channel="channel_in" default-output-channel="channel_default" 
  expression="payload.name" resolution-required="false">
    <int:mapping value="foo" channel="channel_one" />
    <int:mapping value="bar" channel="channel_two" />
</int:router>

答案 2 :(得分:0)

reference docs中所述:

  

从Spring Integration 2.1开始,路由器参数有了更多   跨所有路由器实现标准化。因此,一些   较小的更改可能会破坏旧的基于Spring Integration的应用程序。

     

从Spring Integration 2.1开始,   ignore-channel-name-resolution-failures属性已被删除   resolution-required属性来巩固其行为。   另外,现在需要分辨率的属性默认为true

     

在进行这些更改之前,resolution-required属性默认为   false,导致在没有频道的情况下静默删除邮件   解决,未设置default-output-channel。新行为   需要至少一个已解析的频道,并且默认情况下会引发一个   MessageDeliveryException,如果未确定渠道(或尝试   发送失败)。

     

如果您希望静默删除消息,则可以设置   default-output-channel="nullChannel"

如果您使用的是Java DSL,则配置可能如下所示:

IntegrationFlows.from("process")
        .<JobExecution, String>route(m -> m.getExitStatus().getExitCode(),
                m -> m.channelMapping(ExitStatus.COMPLETED.getExitCode(), "succeed")
                        .defaultOutputChannel("failed")
                        .resolutionRequired(false))
        .get();