PredicatePage edited by Raul Kripalani
Comment:
Enhanced Compound Predicates
Changes (3)
Full ContentPredicatesCamel 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: 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();
Additionally, with PredicateBuilder you can create Regular Expressions and use them as predicates, applying them to the result of an _expression_, e.g. PredicateBuilder.regex(header("foo"), "\d{4}") applies the regular _expression_ to the header = foo. Combining different _expression_ Languages is also possible, e.g.: PredicateBuilder.and(XPathBuilder.xpath("/bookings/flights"), simple("${property.country = 'Spain'}")) The sample below demonstrates further use cases: // 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(); Extensible PredicatesCamel 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.
See Also
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
