骆驼分裂器存在的问题

时间:2016-09-30 18:43:42

标签: apache-camel

我在申请中遇到问题......

当我将XML拆分为4个部分时,我希望将它们全部放在另一个XML中,但它会覆盖并且只覆盖最后一个

.split(body(String.class).tokenizePair("TITTLE",
        "ISSN",false)).log("Line: ${body}")
        .to("file:E:/tst")
//method that I created for use AggregatorStrategy
if (oldExchange == null) {
        System.out.println(newExchange.getIn().getBody(String.class));
        return newExchange;
    }
    String oldBody = oldExchange.getIn().getBody(String.class);
    String newBody = newExchange.getIn().getBody(String.class);
    oldExchange.getIn().setBody(oldBody + "+ " + newBody);
    System.out.println(oldExchange.getIn().getBody(String.class));
    return oldExchange;
}

System.out.println返回: Return

但在我的最终文件中只有: Final File

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您必须使用具有聚合器模式的拆分器。

.split(body(String.class).tokenizePair("TITTLE",
        "ISSN",false), new CustomAggregatorStrategy()).log("Line: ${body}")
        .to("file:E:/tst")

CustomAggregatorStrategy 应该实现AggregationStrategy,并以聚合方式提供新的交换和旧交换。

每次拆分器完成作业然后聚合新的交换和旧交换,最后你将获得拆分处理的所有输出的列表。

您的方法应该是

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                Message newIn = newExchange.getIn();
                Object newBody = newIn.getBody();
                ArrayList list = null;
                if (oldExchange == null) {
                        list = new ArrayList();
                        list.add(newBody);
                        newIn.setBody(list);
                        return newExchange;
                } else {
                        Message in = oldExchange.getIn();
                        list = in.getBody(ArrayList.class);
                        list.add(newBody);
                        return oldExchange;
                }
        }

答案 1 :(得分:0)

尝试.to(" file:E:/ tst?fileExist =追加")