Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

For example as shown in the route below where we use the Load Balancer inside the Content Based Router in the first when:

Code Block
java
java
titleCode will not compilejava
from("direct:start")
    .choice()
        .when(body().contains("Camel"))
            .loadBalance().roundRobin().to("mock:foo").to("mock:bar")
        .otherwise()
            .to("mock:result");

...

To indicate when the balancing stops, you should use .end() to denote the end. So the route is updates as follows:

Code Block
java
java
titleCode will still not compilejava
from("direct:start")
    .choice()
        .when(body().contains("Camel"))
            .loadBalance().roundRobin().to("mock:foo").to("mock:bar").end()
        .otherwise()
            .to("mock:result");

But the code will still not compile. The reason is we have stretched how far we can take the good old Java language in terms of DSL. In a more modern language such as Scala or Groovy you would be able to let it be stack based, so the .end() will pop the last type of the stack, and you would return back to the scope of the Content Based Router. However that's not easily doable in Java. So we need to help Java a bit, which you do by using .endChoice(), which tells Camel to "pop the stack" and return back to the scope of the Content Based Router.

Code Block
java
java
titleCode compilesjava
from("direct:start")
    .choice()
        .when(body().contains("Camel"))
            .loadBalance().roundRobin().to("mock:foo").to("mock:bar").endChoice()
        .otherwise()
            .to("mock:result");

...