Bean BindingPage edited by Claus IbsenChanges (2)
Full ContentBean BindingThe Bean 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, order if importance:
In case where Camel will not be able to choose a method to invoke an AmbiguousMethodCallException is thrown. By default the return value is set on the outbound message body. Using type qualifier to pin-point method to use when having overloaded methodsAvailable as of Camel 2.8 If you have a Bean which has overloaded methods you can now specify the 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(String,String)") .to("mock:result"); By default Camel will match the type name using the simple name, eg 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 do as follows:
.bean(OrderService.class, "doSomething(com.foo.MyOrder)")
Parameter bindingWhen a method have been chosen to be invoked Camel will bind to the parameters of the method. The following Camel specific types is automatic binded:
So if you declare any of the given type above they will be provided by Camel. A note on the Exception is that it will bind to the caught exception of the Exchange. So its often usable if you use a Pojo to handle a given using using eg an onException route. What is most interresting 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 we for instance declare e parameter as: String body then Camel will bind the IN body to this type. Camel will also automatic type convert to the given type declared. Okay lets show some examples. Below is just a simple method with a body binding. Camel will bind the IN body to the body parameter and convert it to a String type. public String doSomething(String body) And in this sample we got one of the automatic binded type as well, for instance the Registry that we can use to lookup beans. public String doSomething(String body, Registry registry) And we can also use Exchange as well: public String doSomething(String body, Exchange exchange) You can have multiple types as well public String doSomething(String body, Exchange exchange, TypeConverter converter) And imagine you use a Pojo to handle a given custom exception InvalidOrderException then we can bind that as well: public String badOrder(String body, InvalidOrderException invalid) So what about headers and other stuff? Well now it gets a bit tricky so we can use annotations to help us. See next section for details. Binding AnnotationsYou can use the Parameter Binding Annotations to customize how parameter values are created from the Message ExamplesFor example a Bean such as: public class Bar { public String doSomething(String body) { // process the in body and return whatever you want return "Bye World"; } Or the Exchange example. Notice that the return type must be void when there is only a single parameter: public class Bar { public void doSomething(Exchange exchange) { // process the exchange exchange.getIn().setBody("Bye World"); } @HandlerAvailable as of Camel 2.0 You can mark a method in your bean with the @Handler annotation to indicate that this method should be used for Bean Binding. public class Bar { @Handler public String doSomething(String body) { // process the in body and return whatever you want return "Bye World"; } POJO consumingFor example you could use POJO Consuming to write a bean like this
public class Foo { @Consume(uri = "activemq:my.queue") public void doSomething(String body) { // process the inbound message here } } Here Camel with subscribe to an ActiveMQ queue, then convert the message payload to a String (so dealing with TextMessage, ObjectMessage and BytesMessage in JMS), then process this method.
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
- [CONF] Apache Camel > Bean Binding confluence