Apache Camel 的 Aggregator2 将 XML 文档聚合成单个大文档

发布于 2024-11-04 02:03:25 字数 1334 浏览 2 评论 0原文

是否可以聚合多个小型 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
  }
}

Is that possible to aggregate multiple small XML documents:

<doc><field name="XXX">fieldValue</field><doc>

using aggregator2 (camel 2.7.0) into one big document

<result><doc>...</doc><doc>...</doc><doc>...</doc>...<doc>...</doc></result>

without using some custom aggregator processor? I've managed to get it done creating custom aggregator, but now I'm simplifying my code, so would like to get rid of it if camel supports that out of the box.

My custom aggregator looks like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-11-11 02:03:26

自定义聚合器处理器是指自定义聚合策略吗?如果是这样的话,那就不行了。目前这是必需的。

我们的路线图是为聚合提供一个 pojo 模型,这样您就不需要使用 Camel API。因此,希望将来这会变得更简单。

By custom aggregator processor, you mean a custom AggregationStrategy? If that's the case, then no. Currently that's required.

We have on the roadmap to offer a pojo model for the aggregation so you dont need to use that Camel API. So expect this to be simpler in the future.

绝影如岚 2024-11-11 02:03:25

好的,您正在使用分组交换选项。然后就有点不同了。数据作为属性存储在交换中的列表中。

您可以使用 POJO 并将参数绑定到属性,而不是处理器。但 List 仍然包含 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;
}

Okay so you are using grouped exchange option. Then its a bit different. The data is stored as a property on the exchange as a list.

Instead of the processor you can use a POJO and bind a parameter to the property. But the List still contains Exchange objects so you need to invoke the getIn().getBody() methods on it. But if you do it like this you don't need to import any Camel API in the POJO.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文