1

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")
}
HongAnTC AnAn
  • 123
  • 2
  • 13
  • Other option is to use Splitter and for each Soapenv call direct:a. – Leo Prince Jan 07 '14 at 09:53
  • Hi Leo, I used Splitter and the problem is my input file is splitted in several messages based on the criteria, but I want a whole file so that I can create a new output file. – HongAnTC AnAn Jan 08 '14 at 01:59

3 Answers3

7

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()
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
Ralf
  • 6,735
  • 3
  • 16
  • 32
  • Perhaps the `.choice()` blocks need `.end()` also? – vikingsteve Jan 07 '14 at 07:19
  • @vikingsteve Don't know. I am not very familiar with the Java DSL. The documentation page for the [content based router](http://camel.apache.org/content-based-router.html) does not show it. If you could confirm that it is required I would of course update the answer. – Ralf Jan 07 '14 at 07:28
  • Thanks Ralf and VikkingSteve, the problem solved by your answer. – HongAnTC AnAn Jan 08 '14 at 01:54
0

There is nothing wrong with the DSL and you dontt need end blocks here. I would look at your data and trace through why all calls are ending up in the same when block. Put a couple of log lines in or enable the tracer and look at the exchanges going through.

Robert Simmons Jr.
  • 1,182
  • 8
  • 21
-1

In Camel root choice() if you have multiple when() cases you have to write otherwise(). Please refer below.

 from("direct:a")
          .choice()
              .when(header("foo").isEqualTo("bar"))
                   .to("direct:b")
              .when(header("foo").isEqualTo("cheese"))
                   .to("direct:c")
              .otherwise()
                   .to("direct:d")
         .end;

The above mentioned solution will check all three conditions even if first one pass.

Ramu
  • 1