ChoiceDefinition is the Apache Camel model class behind the choice() DSL method. It implements the Content-Based Router pattern: a message is tested against a list of when() predicates in order, goes down the branch of the first one that matches, and falls to otherwise() if none do.
You rarely instantiate it yourself. Calling choice() in a route returns a ChoiceDefinition, and the chained when() and otherwise() calls configure it.
In the Java DSL, every route step is a model class in org.apache.camel.model. choice() creates a ChoiceDefinition that holds a list of WhenDefinition clauses and an optional OtherwiseDefinition. At route start-up, Camel turns that model into runtime processors.
Key behaviour:
when() clauses are not evaluated.when() has its own sub-route, which can contain any processors.otherwise() is optional. Without it, a message that matches nothing continues past the choice unchanged.A body containing "urgent" is sent to direct:urgent. Because evaluation stops at the first match, a body containing both words goes only to direct:urgent. Anything else lands in direct:default.
when() accepts any Camel Predicate. The most common choices:
| Predicate style | Example |
|---|---|
| Simple language | simple("${header.type} == 'invoice'") |
| Header builder | header("type").isEqualTo("invoice") |
| Body builder | body().isNull() |
JSONPath (needs camel-jsonpath) | jsonpath("$.amount > 1000") |
| XPath | xpath("/order/@priority = 'high'") |
| Java lambda | exchange -> exchange.getIn().getBody(String.class).isBlank() |
A route that mixes header and content tests:
A branch can hold several steps. Here the large-payment branch logs and then forwards.
Steps written after otherwise() are part of the otherwise branch, which is a frequent source of bugs. Call end() to close the choice so later steps run for every message:
Without .end(), the log and the audit endpoint would run only for non-VIP messages. For a nested choice() inside a when(), close the inner one with end() as well, and use endChoice() when you need to return from a nested processor block to the enclosing choice. Deep nesting is hard to read; move inner logic to a separate route called with direct:.
Use camel-test with MockEndpoint to assert where messages go. Send one message per branch and check each mock received it, including a case that only matches otherwise(). Also test the overlap case, where two predicates are true, to confirm ordering behaves as you intend.
filter() drops messages that do not match. choice() routes each message to one of several branches, optionally with a default.
No. Only the first matching when() runs. To send a message to several destinations, use a multicast or recipient list.
No. If it is absent and nothing matches, the message continues to the step after the choice.
You likely forgot end(). Steps after otherwise() belong to that branch until you close the choice.
ChoiceDefinition is the model behind choice(), Camel's implementation of the Content-Based Router. Order your when() predicates carefully since the first match wins, add otherwise() for a default, close the block with end(), and keep nested choices shallow.