apache-camelcontent-based-retrieval

Camel: How to go all "when" in "choice when"


I need to ask a problem on the operator "choice when" in Apache Camel route. In the following example, if I have two soap-env:Order elements which have 1, 2 value, then I want to create two xml file named output_1.xml and output_2.xml. However, the code can only create one file output_1.xml. Can anyone give me any ideas or hints? Thanks for any help.

    public void configure() {
    ...  
    from("direct:a")
        .choice()
            .when(ns.xpath("//soap-env:Envelope//soap-env:Order='1'"))
                .to("file://data?fileName=output_1.xml")
            .when(ns.xpath("//soap-env:Envelope//soap-env:Order='2'"))
                .to("file://data?fileName=output_2.xml")
            .when(ns.xpath("//soap-env:Envelope//soap-env:Order='3'"))
                .to("file://data?fileName=output_3.xml")
}

Solution

  • My understanding is that the content based router implements "if - else if - else" semantics, meaning that as soon as one test evaluates to true, then the remaining tests are skipped. If you want to create files for every case that returns true then you'd have to change the route to something like this:

    from("direct:a")
        .choice()
           .when(ns.xpath("//soap-env:Envelope//soap-env:Order='1'"))
               .to("file://data?fileName=output_1.xml")
        .end()
        .choice()
           .when(ns.xpath("//soap-env:Envelope//soap-env:Order='2'"))
               .to("file://data?fileName=output_2.xml")
        .end()
        .choice()
            .when(ns.xpath("//soap-env:Envelope//soap-env:Order='3'"))
               .to("file://data?fileName=output_3.xml")
        .end()