Bean BindingPage edited by Reuben Garrett
Comment:
native english readability enhancements
Changes (56)
Full ContentBean BindingBean Binding in Camel defines both which methods are invoked and also how the Message is converted into the parameters of the method when it is invoked. Choosing the method to invokeThe binding of a Camel Message to a bean method call can occur in different ways, in the following order of importance:
In cases where Camel cannot choose a method to invoke, an AmbiguousMethodCallException is thrown. By default the return value is set on the outbound message body. Parameter bindingWhen a method has been chosen for invokation, Camel will bind to the parameters of the method. The following Camel-specific types are automatically bound:
So, if you declare any of these types, they will be provided by Camel. Note that Exception will bind to the caught exception of the Exchange - so it's often usable if you employ a Pojo to handle, e.g., an onException route. What is most interesting is that Camel will also try to bind the body of the Exchange to the first parameter of the method signature (albeit not of any of the types above). So if, for instance, we declare a parameter as String body, then Camel will bind the IN body to this type. Camel will also automatically convert to the type declared in the method signature. Let's review some examples: Below is a simple method with a body binding. Camel will bind the IN body to the body parameter and convert it to a String. public String doSomething(String body)
Any other value is consider to be a type declaration instead - see the next section about specifying types for overloaded methods. When invoking a Bean you can instruct Camel to invoke a specific method by providing the method name:
.bean(OrderService.class, "doSomething")
Here we tell Camel to invoke the doSomething method - Camel handles the parameters' binding. Now suppose the method has 2 parameters, and the 2nd parameter is a boolean where we want to pass in a true value: public void doSomething(String payload, boolean highPriority) { ... } This is now possible in Camel 2.9 onwards:
.bean(OrderService.class, "doSomething(*, true)")
In the example above, we defined the first parameter using the wild card symbol *, which tells Camel to bind this parameter to any type, and let Camel figure this out. The 2nd parameter has a fixed value of true. Instead of the wildcard symbol we can instruct Camel to use the message body as shown:
.bean(OrderService.class, "doSomething(${body}, true)")
The syntax of the parameters is using the Simple _expression_ language so we have to use ${ } placeholders in the body to refer to the message body. If you want to pass in a null value, then you can explicit define this in the method option as shown below:
.to("bean:orderService?method=doSomething(null, true)")
Specifying null as a parameter value instructs Camel to force passing a null value. Besides the message body, you can pass in the message headers as a java.util.Map:
.bean(OrderService.class, "doSomethingWithHeaders(${body}, ${headers})")
You can also pass in other fixed values besides booleans. For example, you can pass in a String and an integer:
.bean(MyBean.class, "echo('World', 5)")
In the example above, we invoke the echo method with two parameters. The first has the content 'World' (without quotes), and the 2nd has the value of 5. Having the power of the Simple language allows us to bind to message headers and other values such as:
.bean(OrderService.class, "doSomething(${body}, ${header.high})")
You can also use the OGNL support of the Simple _expression_ language. Now suppose the message body is an object which has a method named asXml. To invoke the asXml method we can do as follows:
.bean(OrderService.class, "doSomething(${body.asXml}, ${header.high})")
Instead of using .bean as shown in the examples above, you may want to use .to instead as shown:
.to("bean:orderService?method=doSomething(${body.asXml}, ${header.high})")
Using type qualifiers to select among overloaded methodsAvailable as of Camel 2.8 If you have a Bean with overloaded methods, you can now specify parameter types in the method name so Camel can match the method you intend to use. MyBean public static final class MyBean { public String hello(String name) { return "Hello " + name; } public String hello(String name, @Header("country") String country) { return "Hello " + name + " you are from " + country; } public String times(String name, @Header("times") int times) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < times; i++) { sb.append(name); } return sb.toString(); } public String times(byte[] data, @Header("times") int times) { String s = new String(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < times; i++) { sb.append(s); if (i < times - 1) { sb.append(","); } } return sb.toString(); } public String times(String name, int times, char separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < times; i++) { sb.append(name); if (i < times - 1) { sb.append(separator); } } return sb.toString(); } } Then the MyBean has 2 overloaded methods with the names hello and times. So if we want to use the method which has 2 parameters we can do as follows in the Camel route: Invoke 2 parameter method from("direct:start") .bean(MyBean.class, "hello(String,String)") .to("mock:result"); We can also use a * as wildcard so we can just say we want to execute the method with 2 parameters we do Invoke 2 parameter method using wildcard from("direct:start") .bean(MyBean.class, "hello(*,*)") .to("mock:result"); By default Camel will match the type name using the simple name, e.g. any leading package name will be disregarded. However if you want to match using the FQN, then specify the FQN type and Camel will leverage that. So if you have a com.foo.MyOrder and you want to match against the FQN, and not the simple name "MyOrder", then follow this example:
.bean(OrderService.class, "doSomething(com.foo.MyOrder)")
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence
- [CONF] Apache Camel > Bean Binding confluence