Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Bean Binding

The 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 invoke

The binding of a Camel Message to a bean method call can occur in different ways, order if importance:

  • if the message contains the header CamelBeanMethodName (org.apache.camel.MethodName in Camel 1.x) then that method is invoked, converting the body to whatever the argument is to the method.
    • From Camel 2.8 onwards you can qualify parameter types to exact pin-point which method to use when using overloaded methods with the same name (see further below for more details).
    • From Camel 2.9 onwards you can specify parameter values directly in the method syntax option (see further below for more details).
  • the method name can be specified explicitly in the DSL or when using POJO Consuming or POJO Producing
  • Camel 2.0: if the bean has a method that is marked with @Handler annotation then that method is selected
  • if the bean can be converted to a Processor using the Type Converter mechanism then this is used to process the message. This mechanism is used by the ActiveMQ component to allow any JMS MessageListener to be invoked directly by Camel without having to write any integration glue code. You can use the same mechanism to integrate Camel into any other messaging/remoting frameworks.
  • if the body of the message can be converted to a BeanInvocation (the default payload used by the ProxyHelper) - then that its used to invoke the method and pass the arguments
  • otherwise the type of the method body is used to try find a method which matches; an error is thrown if a single method cannot be chosen unambiguously.
  • you can also use Exchange as the parameter itself, but then the return type must be void.

...

By default the return value is set on the outbound message body.

Parameter binding

When a method have been chosen to be invoked Camel will bind to the parameters of the method.

...

  • org.apache.camel.Exchange
  • org.apache.camel.Message
  • Camel 2.0: org.apache.camel.CamelContext
  • org.apache.camel.TypeConverter
  • Camel 2.0: org.apache.camel.spi.Registry
  • java.lang.Exception

...

So what about headers and other stuff? Well now it gets a bit tricky so we can use annotations to help us, or specify the binding in the method name option.
See next section the following sections for more details.

Binding Annotations

You can use the Parameter Binding Annotations to customize how parameter values are created from the Message

...

Code Block
public class Bar {

    public void doSomething(Exchange exchange) {
      // process the exchange
      exchange.getIn().setBody("Bye World");
   }

@Handler

Available 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.
This has the advantage as you do not have to specify the method name in the Camel route. And thus you do not run into problems when you rename the method name using an IDE that don't find all references.

Code Block
java
java
public class Bar {

    @Handler
    public String doSomething(String body) {
      // process the in body and return whatever you want
      return "Bye World";
   }

POJO consuming

For example you could use POJO Consuming to write a bean like this

Info
title@Consume requires camel-spring

Using the @Consume annotations requires camel-spring that uses the org.apache.camel.spring.CamelBeanPostProcessor to perform the setup for this consumer and the needed bean bindings.

Tip
title@MessageDriven is @deprecated

The @MessageDriven has been replaced with @Consume in Camel 1.5.0 or newer. Its now marked as @deprecated and will be removed in Camel 2.0.

Code Block

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.

Using type qualifier to pin-point method to use when having overloaded methods

Available 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.
Given the following bean:

Wiki Markup
{snippet:id=e1|lang=java|title=MyBean|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

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:

Wiki Markup
{snippet:id=e2|lang=java|title=Invoke 2 parameter method|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

We can also use a * as wildcard so we can just say we want to execute the method with 2 parameters we do

Wiki Markup
{snippet:id=e3|lang=java|title=Invoke 2 parameter method using wildcard|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

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:

Code Block

   .bean(OrderService.class, "doSomething(com.foo.MyOrder)")
Info

The current implementation for choosing method using type qualifiers only compares the type names. It does not check any instanceof checks or the likes. The type name must match exactly, as its using a string equals comparison.

Specifying parameter values in method name syntax

Parameter binding using method option

Available as of Camel 2.9

Camel uses the following rules to determine if its a parameter value in the method option

  • The value is either true or false which denotes a boolean value
  • The value is a numeric value such as 123 or 7
  • The value is a String enclosed with either single or double quotes
  • It can be evaluated using the Simple language, which means you can use eg body, header.foo and other Simple tokens.

Any other value is consider to be a type declaration instead, see next section about pin pointing types for overloaded methods.Available as of Camel 2.9

When invoking a Bean you can instruct Camel to invoke a specific method by providing the method name. For example as shown below:

...

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

Its a good idea to use ${ } placeholders in the method option as shown in the example above. This makes it clear to the read, that this is a Simple token and the actual value is dynamic computed form the Exchange being routed.

You can also pass in other fixed values than boolean values. For example to pass in an String and integer do as follows:

...

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

Using type qualifier to pin-point method to use when having overloaded methods

Available 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.
Given the following bean:

Wiki Markup
{snippet:id=e1|lang=java|title=MyBean|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

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:

Wiki Markup
{snippet:id=e2|lang=java|title=Invoke 2 parameter method|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

We can also use a * as wildcard so we can just say we want to execute the method with 2 parameters we do

Wiki Markup
{snippet:id=e3|lang=java|title=Invoke 2 parameter method using wildcard|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java}

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:

Code Block

   .bean(OrderService.class, "doSomething(com.foo.MyOrder)")
Info

Camel currently only supports either specifying parameter binding or type per parameter in the method name option. You cannot specify both at the same time, such as

Code Block

doSomething(com.foo.MyOrder ${body}, boolean ${header.high})

This may change in the future.