【问题标题】:Camel Choice by Example骆驼选择示例
【发布时间】:2014-01-07 06:37:45
【问题描述】:

我有 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.
}

我有一个我编写的 Camel 处理器,它将产生一个 ValidatedWitches 实例(同样,它由 2 个 List&lt;Witch&gt; 组成):

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);
    }
}

我现在想以一种方式路由我的好女巫名单,另一种方式我的坏女巫名单:

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

我可以在我的&lt;choice&gt; 中放入什么来获取ValidatedWitches.getGoodWitches() 并将它们路由到direct:goodWitches,并获取ValidatedWitches.getBadWitches() 并将它们路由到direct:badWitches?

【问题讨论】:

    标签: java apache-camel routes choice


    【解决方案1】:

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

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

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

    那么你的路线可能看起来像这样:

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

    关键点:

    • 拆分器应返回两个或多个对象的集合
    • 选择需要能够区分分割对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2012-05-19
      • 2014-09-14
      • 1970-01-01
      • 2018-09-09
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多