How to use compound predicates in Apache Camel.
Roger is starting to grok Camel. And yes I admit it can take a little time to get the feeling and hang of it, but then you really get these "AHA/this is so cool" experiences.
Roger wants to do content based routing, that is a very common EIP pattern to use. And Camel have first class support for this using its strong Predicate and Expression types.
Roger wants to test if his HL7 message is either a ORM message type and the trigger event is 001.
The problem is how to express the AND in the compound predicate?
Camel has support for combining predicate using all kind of binary operators in its: org.apache.camel.builder.PredicateBuilder class.
As the fluent builder can get a bit complex I like to divide, so we define our predicates outside the route:
Predicate p1 = header("hl7.msh.messageType").isEqualTo("ORM"):
Predicate p2 = header("hl7.msh.triggerEvent").isEqualTo("001");
Now we have our predicates and we can use the PredicateBuilder to create our compound predicate:
Predicate isOrm = PredicateBuilder.and(p1, p2);
And then we can refer to the isOrm predicate in our route.
So Rogers route can be written as:
Inlining the compound predicates can be a bit cumbersome as the Java compiler can get a bit spooked when the fluent builders get long and complex. That is why we encourage you to split your route such as above. It also enhances the readably as the route above is a kind of high level diagram without all the minor details of predicates and expressions.