Apache Camel聚合器:输入和输出

时间:2013-12-19 16:42:45

标签: java io apache-camel aggregation

我有以下路线:

<route id="my-aggregator">
    <from uri="direct:aggregator" />

    <aggregate strategyRef="myAggregationStrategy" completionSize="3">
        <correlationExpression>
            <simple>${header.id}</simple>
        </correlationExpression>
        <to uri="bean:payloadProcessor?method=process" />
    </aggregate>
</route>

正如您所看到的,它会等到收到3个都具有匹配header.id值的交换。收到3条此类消息后,将执行以下代码:

public class MyAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange e1, Exchange e2) {
        // The first (of 3) message will have a `List<Widget>` on its body. Extract it here.
        List<Widget> redWidgets = ??? // e1.getIn().getBody()? e2.getIn().getBody()? something else?

        // The second (of 3) message will have a `List<Widget>` on its body. Extract it here.
        List<Widget> blueWidgets = ??? // e1.getIn().getBody()? e2.getIn().getBody()? something else?

        // The third (of 3) message will have a `List<Widget>` on its body. Extract it here.
        List<Widget> greenWidgets = ??? // e1.getIn().getBody()? e2.getIn().getBody()? something else?

        WidgetPayload payload = new WidgetPayload(redWidgets, blueWidgets, greenWidgets);

        // Which "output" exchange do I set payload to, so that it gets routed on to the
        // "payloadProcessor" bean?
        e1.getOut().setBody(payload);   /* or */    e2.getOut().setBody(payload);

        // What do we even return?
        return e1; /* or return e2; */
    }
}

所以在聚合器中,我试图访问通过各种路由发送到聚合器的每个List<Widget>,将它们组合成WidgetPayload实例,然后设置{{1作为出站交换的出站/返回值。但我无法弄清楚这一切的正确方法。具体做法是:

  1. 如何从WidgetPayloadList<Widget>提取3 e1中的每一个?
  2. e2e1甚至代表什么?进出?新旧?还有别的吗?
  3. 我在e2 / e1上调用了哪种方法,以便将e2转发到payload bean?

1 个答案:

答案 0 :(得分:2)

查看我提到的页面的代码示例;第一个参数是聚合交换。在第一次调用聚合时为空。您必须在第一次调用时创建WidgetPayload,然后在每次后续调用时继续向其添加小部件。然后,当您到达聚合器之后的路径步骤时,交换的主体就是您聚合的WidgetPayload。