Viktor Stoitschev
Viktor Stoitschev

Reputation: 402

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

The problem:

I need to process different huge XML files. Each file contains a certain node which I can use to identify the incoming xml message by. Based on the node/tag the message should be send to a dedicated recipient.

The XML message should not be converted to String and then checked with contains as this would be really inefficient. Rather xpath should be used to "probe" the message for the occurrence of the expected node.

The solution should be based on camel's Java DSL. The code:

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

suggested in Camel's Doc does not compile. I am using Apache Camel 2.19.0.

Upvotes: 2

Views: 560

Answers (1)

Gerry Mantha
Gerry Mantha

Reputation: 1098

This compiles:

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

You need the .when() to test predicate expressions when building a content-based-router.

Upvotes: 2

Related Questions