Camel Filter Method Signatures

I have an Order POJO, and on my Camel route, I would like to pass each Order instance (message) through a filter as follows:

 ExpensiveOrderFilter eof = new ExpensiveOrderFilter(); from("direct:whatever") .filter().method(eof) .to("direct:wherever"); 

The filter should only allow Order , although if their Order#getPrice() more than $ 100.

 public class ExpensiveOrderFilter { public void filterCheapOrders(Order order) { if(order.getPrice() < 100.00) ??? else ??? } } 

What should the filterCheapOrders method filterCheapOrders so that it correctly filters out the "cheap" (<$ 100) orders, preventing them from being redirected to direct:wherever ? Thanks in advance!

+4
source share
1 answer

There are two parts. First, method(..) is a Camel expression type called a predicate. Any valid method that you call should return a boolean, therefore:

 public class ExpensiveOrderFilter { public boolean isCheapOrder(Order order) { return order.getPrice() < 100.00; } } 

The Order parameter will be injected into the best attempt by a Camel mechanism called bean binding , which will try to convert the message body to Order. If this fails, the route will throw an exception.

You name the method that should be called in the bean in the method(..) block:

 .filter().method(eof, "isCheapOrder") 

Only cheap orders will be continued. For simple expressions, you can also consider the simple expression language built into Camel and skip the bean entry:

 .filter().simple("${body.price} < 100") 
+6
source

All Articles