Versions Compared

Key

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

...

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

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.

Parameter binding

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

...

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

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")

Here we tell Camel to invoke the doSomething method. How the parameters is bound is handled by Camel. Now suppose the method has 2 parameters, and the 2nd parameter is a boolean, where we want to pass in a true value, such as the method signature below:

Code Block

public void doSomething(String payload, boolean highPriority) {
   ...
}

This is now possible in Camel 2.9 onwards:

Code Block

   .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 wild card symbol we can instruct Camel to use the message body as shown:

Code Block

   .bean(OrderService.class, "doSomething(body, true)")

The syntax of the parameters is using the Simple expression language so we can use ${ } placeholders to make this more expressive:

Code Block

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

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

   .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 the quotes). And the 2nd the value of 5.
Camel will automatic type convert the values to the parameter types.

Having the power of the Simple language allows us to bind to message headers and other values such as:

Code Block

   .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:

Code Block

   .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:

Code Block

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