2

我们使用拆分器来遍历压缩文件中的文件,同时,我们还使用自定义聚合器,它为我们提供了一个主体列表 - 该主压缩文件中的两个文件。现在,在拆分之后,我想提取在聚合块处理期间设置的标头,该处理发生在聚合器的结果上。但是,聚合器的输出似乎丢失了,在拆分块之后我什么也没有回来。

我敢肯定我没有得到这方面的基础知识。如果有人可以在这里提供帮助,将不胜感激。

<route id="main-route">
        <split streaming="true">
            <ref>zipSplitter</ref>
            <choice>
                <when>
                    <method bean="fileHandler" method="isY" />
                    <to uri="direct:y" />
                </when>
                <otherwise>
                    <to uri="direct:x" />
                </otherwise>
            </choice>
            <to uri="direct:aggregate" />
       </split>
       <!--Do something by extracting the headers set during the processing underneath in the aggregation block i.e. process-data -->
</route>
<route id="aggregate-data">
        <from uri="direct:aggregate" />
        <camel:aggregate strategyRef="aggregationStrategy" completionSize="2">
            <camel:correlationExpression>
                <constant>true</constant>
            </camel:correlationExpression>
            <to uri="direct:process-data"/>
        </camel:aggregate>
 </route>

聚合器-

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    Object newBody = newExchange.getIn().getBody();
    Map<String, Object> newHeaders = newExchange.getIn().getHeaders();

    ArrayList<Object> list = null;
    if (oldExchange == null) {
        list = new ArrayList<Object>();
        list.add(newBody);
        newExchange.getIn().setBody(list);
        return newExchange;
    } else {
        Map<String, Object> olderHeaders = oldExchange.getIn().getHeaders();
        olderHeaders.putAll(newHeaders);
        list = oldExchange.getIn().getBody(ArrayList.class);

        list.add(newBody);
        return oldExchange;
    }
}
4

1 回答 1

1

您必须将聚合逻辑保留在拆分范围内。应该有一个聚合实例为您的拆分进行聚合,如下所示,

     <route id="main-route">
        <split streaming="true" strategyRef="aggregationStrategy">
            <ref>zipSplitter</ref>
            <choice>
                <when>
                    <method bean="fileHandler" method="isY" />
                    <to uri="direct:y" />
                </when>
                <otherwise>
                    <to uri="direct:x" />
                </otherwise>
            </choice>               
       </split>
  </route>

您必须在拆分标记中指定聚合策略作为属性,如上面的代码。这样,每次迭代的交换回报将在聚合策略 bean 中可用以进行聚合。

希望能帮助到你 :)

于 2015-08-28T06:46:26.703 回答