Choice Processor

The Choice processor is a conditional statement that can be employed to route incoming messages to alternative producer endpoints. Each of these alternative producer endpoints will be preceded by a when method, which takes a predicate-argument. If the predicate happens to be true, the following target is selected, otherwise, processing will proceed to the next when in the rule.

XML DSL:

<camelContext id="buildSimpleRouteWithChoice" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="SourceURL"/> <choice> <when> <!-- First predicate --> <simple>header.foo = 'bar'</simple> <to uri="Target1"/> </when> <when> <!-- Second predicate --> <simple>header.foo = 'manchu'</simple> <to uri="Target2"/> </when> <otherwise> <to uri="Target3"/> </otherwise> </choice> </route> </camelContext>

 

Java DSL:

from("direct:start") .choice() .when(bodyAs(String.class).contains("Camel")) .loadBalance().roundRobin().to("mock:foo").to("mock:bar").endChoice() .otherwise() .to("mock:result");