Integration / Apache Camel Interview Questions
How does the Content-Based Router EIP work in Camel (choice/when/otherwise)?
The Content-Based Router (CBR) routes each incoming message to exactly one destination based on its content. In Camel it is implemented with choice() — when(predicate) — otherwise() — end(). Only the first matching when() branch executes; otherwise() catches messages that match no predicate.
from("direct:orders")
.choice()
.when(header("region").isEqualTo("EU"))
.to("jms:queue:eu-orders")
.when(header("region").isEqualTo("US"))
.to("jms:queue:us-orders")
.when(simple("${header.priority} == HIGH"))
.to("jms:queue:priority-orders")
.otherwise()
.to("jms:queue:default-orders")
.end();Predicates can use Simple, XPath, JSONPath, SpEL, or custom expressions. After .end() the route continues the normal flow. To continue processing AFTER every branch (not just the matching one), use endChoice() and add further steps. The when() predicate has access to the full Exchange including headers, body, and properties.
More Related questions...