是否可以聚合多个小XML文档:
<doc><field name="XXX">fieldValue</field><doc>在一个大文档中使用aggregator2 (camel 2.7.0)
<result><doc>...</doc><doc>...</doc><doc>...</doc>...<doc>...</doc></result>而不使用一些定制的聚合器处理器?我已经设法完成了创建自定义聚合器的工作,但现在我正在简化我的代码,所以如果camel支持开箱即用的话,我想摆脱它。
我的自定义聚合器如下所示:
class DocsAggregator implements Processor {
void process(Exchange exchange) {
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def Document parentDoc = builder.parse(new ByteArrayInputStream("<?xml version='1.0'?><add></add>".toString().bytes));
def groupedExchanges = exchange.properties.find {it.key == 'CamelGroupedExchange'}
groupedExchanges.value.each { Exchange x ->
def Document document = x.'in'.body
def bos = new ByteArrayOutputStream()
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(bos))
def node = document.documentElement.childNodes.find { Node it -> it.nodeType == Node.ELEMENT_NODE}
def cloned = parentDoc.adoptNode(node)
parentDoc.documentElement.appendChild(cloned)
}
exchange.in.body = parentDoc
}
}发布于 2011-04-29 13:53:54
好的,所以你使用的是分组交换选项。那就有点不同了。数据以列表的形式存储在交换上的属性中。
您可以使用POJO并将参数绑定到属性,而不是使用处理器。但是该列表仍然包含Exchange对象,因此您需要对其调用getIn().getBody()方法。但是如果你这样做,你不需要在POJO中导入任何Camel API。
public Document mergeMyStuff(@Property("CamelGroupedExchange") List grouped) {
Document parent = ...
for (int i = 0; i < grouped.size; i++) {
Document doc = list.get(i).getIn().getBody(Documemt.class);
.. add to parent doc
}
return parent;
}发布于 2011-04-29 01:38:42
所谓定制聚合器处理器,是指定制的AggregationStrategy吗?如果是这样,那就不是。目前,这是必需的。
我们已经在路线图上为聚合提供了pojo模型,因此您不需要使用Camel API。因此,希望这在未来会更简单。
https://stackoverflow.com/questions/5820613
复制相似问题