Java 骆驼选择示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20669070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:29:04  来源:igfitidea点击:

Camel Choice by Example

javaapache-camelrouteschoice

提问by IAmYourFaja

I have 2 POJOs:

我有 2 个 POJO:

public class Witch {
    private Broom broom;
    private List<Spell> spells;

    // constructors, getters/setters, etc.
}

public class ValidatedWitches {
    private List<Witch> goodWitches
    private List<Witch> badWitches;

    // constructors, getters/setters, etc.
}

I have a Camel processor I wrote that will produce a ValidatedWitchesinstance (which again, consists of 2 List<Witch>):

我有一个我写的 Camel 处理器,它会产生一个ValidatedWitches实例(同样由 2 组成List<Witch>):

public class WitchValidator implements Processor {
    @Override
    public void process(Exchange exchange) {
        List<Witch> witchesToValidate = (List<Witch>)exchange.getIn().getBody();

        ValidatedWitches validated = validate(witchesToValidate);

        exchange.getOut().setBody(validated);
    }

    private ValidatedWitches validate(List<Witch> toValidate) {
        // For each witch, determines if it is a good witch, or a bad witch,
        // and places it on an appropriate list.
        List<Witch> good = new ArrayList<Witch>();
        List<Witch> bad = new ArrayList<Witch>();

        // etc...

        return new ValidatedWitches(good, bad);
    }
}

I now want to routemy list of good witches 1 way, and my list of bad witches another way:

我现在想以一种方式路由我的好女巫列表,并以另一种方式路由我的坏女巫列表:

<route id="witch-route">
    <!-- Everything before my WitchValidator component... -->

    <to uri="bean:witchValidator?method=process" />

    <choice>
        <when>
            <simple>???</simple>
            <to uri="direct:goodWitches" />
        </when>
        <when>
            <simple>???</simple>
            <to uri="direct:badWitches" />
        </when>
    </choice>
</route>

What can I put inside my <choice>to take the ValidatedWitches.getGoodWitches()and route them to direct:goodWitches, and to take the ValidatedWitches.getBadWitches()and route them to direct:badWitches?

我可以在我的里面放什么<choice>来获取ValidatedWitches.getGoodWitches()和路由它们direct:goodWitches,以及将ValidatedWitches.getBadWitches()它们路由到direct:badWitches什么?

采纳答案by bgossit

A <choice> is either one or the other, so in your example you can only reach one destination URI. Therefore you may need to add a <split>before the <choice>so that you have two separate messages being evaluated.

A <choice> 是其中之一,因此在您的示例中,您只能到达一个目标 URI。因此,您可能需要在<split>之前添加,<choice>以便评估两个单独的消息。

See splitterfor your options there, for example you may want to create your own POJO with a method that returns a list of two ValidatedWitches objects - one with the "goodWitches" collection populated only and one with the "badWitches" collection populated only.

请参阅splitter那里的选项,例如,您可能希望使用返回两个 ValidatedWitches 对象列表的方法创建自己的 POJO - 一个只填充“goodWitches”集合,一个只填充“badWitches”集合。

There are many predicateoptions available, but an easy one would be to check if each array was empty.

有许多可用的谓词选项,但一个简单的方法是检查每个数组是否为空。

Then your route could look something like this:

那么您的路线可能如下所示:

<to uri="bean:witchValidator?method=process" />
<split>
    <method beanType="SPLITTER_CLASS_NAME" method="SPLITTER_METHOD" />
    <choice>
        <when>
            <simple>${body.goodWitches.size} > 0</simple>
            <to uri="direct:goodWitches" />
        </when>
        <when>
            <simple>${body.badWitches.size} > 0</simple>
            <to uri="direct:badWitches" />
        </when>
    </choice>
</split>

Key points:

关键点:

  • Splitter should return a collection of two or more objects
  • Choice needs to be able to distinguish between the split objects
  • Splitter 应该返回两个或多个对象的集合
  • 选择需要能够区分拆分的对象