Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository: https://github.com/apache/camel/blob/master/README.md https://github.com/apache/camel/blob/master/components/readme.adoc
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
A simple example is to route an Exchange based on a header value:
In the route above the Predicate is the
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:
And then you can refer to it in the route as:
Negating a Predicate
You can use the not method on the
First we import the not static, so it makes our route nice and easy to read:
And then we can use it to enclose an existing predicate and negate it as the example shows:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
from("direct:start") .choice() .when(not(header("username").regex("goofy|pluto"))).to("mock:people") .otherwise().to("mock:animals") .end();
You can also create compound predicates using boolean operators such as
Currently this feature is only available in the Java-based DSLs, but not in the Spring nor Blueprint DSLs.
Combining different Expression Languages is also possible, e.g.:
The sample below demonstrates further use cases:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
// We define 3 predicates based on some user roles // we have static imported and/or from org.apache.camel.builder.PredicateBuilder // First we have a regular user that is just identified having a username header Predicate user = header("username").isNotNull(); // The admin user must be a user AND have a admin header as true Predicate admin = and(user, header("admin").isEqualTo("true")); // And God must be an admin and (either have type god or a special message containing Camel Rider) Predicate god = and(admin, or(body().contains("Camel Rider"), header("type").isEqualTo("god"))); // As you can see with the predicates above we can stack them to build compound predicates // In our route below we can create a nice content based router based on the predicates we // have defined. Then the route is easy to read and understand. // We encourage you to define complex predicates outside the fluent router builder as // it will just get a bit complex for humans to read from("direct:start").choice() .when(god).to("mock:god") .when(admin).to("mock:admin") .when(user).to("mock:user") .otherwise().to("mock:guest") .end();
Camel supports extensible Predicates using multiple Languages; the following languages are supported out of the box
Most of these languages is also supported used as Annotation Based Expression Language.
You can easily write your own plugin predicate by implementing the Predicate interface.
There are also a number of helper builders available such as the PredicateBuilder class
Using Predicates in your IDETo use different expression and predicates in your IDE you need to perform a static import of the builder class for the language(s) you wish to use.