...
Camel supports a pluggable interface called Predicate which can be used to integrate a dynamic predicate into Enterprise Integration Patterns such as when using the Message Filter or Content Based Router.
A Predicate is being evaluated to a boolean value so the result is either true
or false
. This makes Predicate so powerful as it is often used to control the routing of message in which path they should be routed.
A simple example is to route an Exchange based on a header value:
Code Block | ||||
---|---|---|---|---|
| ||||
from("jms:queue:order") .choice() .when().header("type").isEqualTo("widget").to("bean:widgetOrder") .when().header("type").isEqualTo("wombat").to("bean:wombatOrder") .otherwise() .to("bean:miscOrder") .end(); |
In the route above the Predicate is the header("type").isEqualTo("widget")
as its constructed as an Expression that is evaluated as a Predicate. To do this the various Builder classes helps us here to create a nice and fluent syntax. isEqualTo
is a builder method that returns a Predicate based on the input.
Sometimes the fluent builders can get long and a bit complex to read, then you can just define your predicate outside the route and then just refer to the predicate in the route:
Code Block | ||||
---|---|---|---|---|
| ||||
Predicate isWidget = header("type").isEqualTo("widget")
|
And then you can refer to it in the route as:
Code Block | ||||
---|---|---|---|---|
| ||||
from("jms:queue:order")
.choice()
.when(isWidget).to("bean:widgetOrder")
.when(isWombat).to("bean:wombatOrder")
.otherwise()
.to("bean:miscOrder")
.end();
|
Compound Predicates
You can also create compound predicates using boolean operators such as and, or, not
and many others.
The sample below demonstrates this:
TODO: Snippet
Extensible Predicates
Camel supports extensible Predicates using multiple Languages; the following languages are supported out of the box
...
Include Page | ||||
---|---|---|---|---|
|