Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

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.

...

Code Block
java
java
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();  

...

Code Block
java
java
Predicate isWidget = header("type").isEqualTo("widget");

And then you can refer to it in the route as:

Code Block
java
java
from("jms:queue:order")
   .choice()
      .when(isWidget).to("bean:widgetOrder")
      .when(isWombat).to("bean:wombatOrder")
   .otherwise()
      .to("bean:miscOrder")
   .end();  

Negating a Predicate

You can use the not method on the PredicateBuilder to negate a predicate.

First we import the not static, so it makes our route nice and easy to read:

Code Block

import static org.apache.camel.builder.PredicateBuilder.not

And then we can use it to enclose an existing predicate and negate it as the example shows:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceWhenNotPredicateTest.java}

Compound Predicates

You can also create compound predicates using boolean operators such as and, or, not and many others.

Currently this feature is only available in the Java-based DSLs, but not in the Spring nor Blueprint DSLs.

Using the PredicateBuilder class, you can combine predicates from different Expression Languages based on logical operators and comparison operators:

  • not, and, or
  • isNull, isNotNull
  • isEqualTo, isGreaterThan, isLessThan
  • startsWith, endsWith
  • in ("any of X predicates stands true")

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.:

Code Block

PredicateBuilder.and(XPathBuilder.xpath("/bookings/flights"), simple("${property.country = 'Spain'}"))

The sample below demonstrates this:
TODO: Snippetfurther use cases:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java}

Extensible Predicates

Camel supports extensible Predicates using multiple Languages; the following languages are supported out of the box

Include Page
CAMEL:Languages SupportedCAMEL:
Languages Supported

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 IDE

Include Page
CAMEL:Using Languages In IDECAMEL:
Using Languages In IDE

See Also