如何避免null body在多个步骤中进入下一个处理器

时间:2015-08-31 05:32:47

标签: java apache-camel integration

如何限制空体在一个公共位置进入多个处理器。在下面的代码中,不是在每个处理器上放置空体检查,我如何在一个地方定义它?

<choice>
    <when>
        <simple>${body} != null</simple>
        <process ref="processor1" />
        <choice>
            <when>
                <simple>${body} != null</simple>
                <process ref="processor2" />
                <!-- Splitter -->
                <split>
                    <simple>body</simple>
                    <process ref="processor3" />
                </split>
            </when>   
        </choice>
    </when>
</choice>     

3 个答案:

答案 0 :(得分:4)

我建议你将root全部放在一起,这样就会使进一步的null检查变得过时。停止当前邮件路由处理的快捷方法是在Exchange.ROUTE_STOP对象上设置exchange属性并返回null

exchange.getProperty(Exchange.ROUTE_STOP, Boolean.TRUE)

答案 1 :(得分:1)

评论中的代码会被很好地显示出来,所以我发布了一个答案。 在处理器中执行它是一个简单的空检查,没有哲学

public class SomeProcessor implements Processor {

    public void process(Exchange exchange) {
        if(exchange.getIn().getBody() != null){
            // Your processing here
            // Is only performed
            // When body is not null
            // Otherwise null body is being resent
        }
    }

}

编辑(回答评论): AFAIK是不可能的,它不是一个合适的方法。 您已经使用的Router是如何执行的。 如果你想扔掉你的信息,我认为这可行(但我没有检查它):

<choice>
    <when>
        <simple>${body} == null</simple>
        #<stop />
        # OR
        #<to uri="wherever-you-want-to-send-nonvalid-messages" />
    </when>
    <otherwise>
        <camel:process ref="processor1" />
        <camel:process ref="processor2" />
        <camel:process ref="processor3" />
        <to uri="where-you-want-to-send-valid-messages" />
    </otherwise>

</choice>

显然,它只会在第一个处理器之前检查空值,所以如果第二个处理器给出空值消息,它就不会被抛出。

答案 2 :(得分:1)

通过添加<stop>标签,您可以执行此操作。

<choice>
    <when>
        <simple>${body} != null</simple>
        <stop></stop>            
    </when>   
    <otherwise>
        <process ref="processor2" />
        <!-- Splitter -->
        <split>
        <simple>body</simple>
        <process ref="processor3" />
        </split>
    <otherwise>
</choice>