高效的基于 Camel 内容的路由器:根据使用 Java DSL 包含的标签将 XML 消息路由到正确的收件人

Efficient Camel Content Based Router: Route XML messages to the correct recipient based on contained tag with Java DSL

问题:

我需要处理不同的巨大 XML 文件。每个文件都包含一个特定的节点,我可以使用它来识别传入的 xml 消息。基于 node/tag 消息应该发送给专门的收件人。

不应将 XML 消息转换为字符串,然后使用 contains 检查,因为这确实效率低下。而 xpath 应该用于 "probe" 出现预期节点的消息。

解决方案应该基于 camel 的 Java DSL。代码:

from("queue:foo")
.choice().xpath("//foo")).to("queue:bar") 
.otherwise().to("queue:others");

Camel's Doc中的建议不编译。我正在使用 Apache Camel 2.19.0.

这样编译:

    from("queue:foo")
        .choice().when(xpath("//foo"))
            .to("queue:bar") 
        .otherwise()
            .to("queue:others");

构建 content-based-router 时需要 .when() 来测试谓词表达式。