Apache Camel - Bean integration improved

I might as well continue this month with a blog post about a new feature in the upcoming Apache Camel 2.9 release I implemented a couple of weeks ago.

Apache Camel has always had great integration for using beans. You had all kind of options to pick and chose according to your needs and liking.

When Camel invokes a bean, it uses a strategy to adapt to the bean. It introspects the bean and using an algorithm if figures out the method to invoke and how to bind to the parameters of the method signature.
This algorithm is detailed in chapter 4 in the Camel in Action book, figure 4.5 and 4.6.

So what have we added in Camel 2.9? Well one area where the previous logic was a bit lacking was binding to methods with multiple parameters and by which some parameters would have to be instructed using annotations how to bind.

Imagine this method signature

public void doSomething(String payload, boolean highPriority)

What we would like to do from a Camel route is to invoke this method, with the message payload as the first parameter, and true for the 2nd parameter.

This is now more easy in Camel 2.9 as you can declare the binding directly in the Camel route as shown:

.bean(OrderService.class, "doSomething(${body}, true)")

This applies as well when using .to

.to("bean:myOrderService?method=doSomething(${body}, true)")

In fact Camel uses the Simple language in the syntax which allows you to bind message headers, properties, invoke other beans, property placeholder values etc.

In fact you can even invoke methods using the OGNL support from Simple. Now suppose the message body is a Java object which has a method called asXml(), and you would pass the result of this method invocation to the doSomething method. This can simply be done as follows:

.to("bean:myOrderService?method=doSomething(${body.asXml()}, true)")

You can omit the () and this time for the boolean parameter we bind to a message header with the key high

.to("bean:myOrderService?method=doSomething(${body.asXml}, ${header.high}")

You can read more details in the parameter binding using method option section in the Apache Camel documentation.


jamie said...

Really looking forward to the new features!

Viyantham said...

If I have one bean in which param is nested bean, please let me know how to invoke with method from that nested bean using blueprint xml

Param of bean1 is bean2.
Now I have to call method of bean2.
Bean:bean1.bean2?method=method name is not working for me