I have choice with many options, but the route works only for last condition. For other conditions, the route is stuck and wont proceed further.
public class CamelChoiceTest {
private CamelContext context;
@Before
public void initializeContext() throws Exception {
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:test")
.choice()
.when(header("number").isEqualTo("one")).to("direct:one")
.when(header("number").isEqualTo("two")).to("direct:two")
.when(header("number").isEqualTo("three")).to("direct:three")
.endChoice()
.log("only final condition reaches here");
from("direct:one").log("one is selected");
from("direct:two").log("two is selected");
from("direct:three").log("three is selected");
}
};
context = new DefaultCamelContext();
context.addRoutes(builder);
context.setTracing(true);
context.start();
}
private void send(String header){
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setHeader("number", header);
exchange.getIn().setBody("test", String.class);
ProducerTemplate producerTemplate = context.createProducerTemplate();
// Send the request
producerTemplate.send("direct:test", exchange);
}
@Test
public void testOne() throws Exception {
send("one");
}
@Test
public void testTwo() throws Exception {
send("two");
}
@Test
public void testThree() throws Exception {
send("three");
}
}
When executed, the log "only final condition reaches here" is printed for final condition. When conditions are reordered also, it is printing for last condition.
I think it is a problem with Java DSL. When I created the same in XML, it works fine,
<camel:camelContext id="testCamelContext" trace="true"
streamCache="true">
<camel:route>
<camel:from uri="direct:test" />
<camel:choice>
<camel:when>
<camel:simple>${header.number} == 'one'</camel:simple>
<camel:to uri="direct:one" />
</camel:when>
<camel:when>
<camel:simple>${header.number} == 'two'</camel:simple>
<camel:to uri="direct:two" />
</camel:when>
<camel:when>
<camel:simple>${header.number} == 'three'</camel:simple>
<camel:to uri="direct:three" />
</camel:when>
</camel:choice>
<camel:to uri="bean:routeBean?method=receive" />
</camel:route>
</camel:camelContext>
In your example, the when conditions seem to evaluate correctly, however the final log statement is missing for test "one" and "two".
Use .end() instead of .endCoice():
.endChoice() in order to return "back" to the Content Based Router, i.e., use .endChoice() to end a when condition if the code block is not a simple statement, see here for more information about this problem. .end() in order to end the whole choice block.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With