Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: spelling error. invocation not invokation

...

When a method has been chosen for invokationinvocation, Camel will bind to the parameters of the method.

...

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 Pojo to handle, e.g., an onException route.

...

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.

Code Block

public String doSomething(String body)

In the following sample we got one of the automatically-bound types as well - for instance, a Registry that we can use to lookup beans.

Code Block

public String doSomething(String body, Registry registry)

We can use Exchange as well:

Code Block

public String doSomething(String body, Exchange exchange)

You can also have multiple types:

Code Block

public String doSomething(String body, Exchange exchange, TypeConverter converter)

And imagine you use a POJO Pojo to handle a given custom exception InvalidOrderException - we can then bind that as well:

Code Block

public String badOrder(String body, InvalidOrderException invalid)

...

For example, a Bean such as:

Code Block

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 of the type org.apache.camel.Exchange:

Code Block

public class Bar {

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

...

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 an advantage as you need not specify a method name in the Camel route, and therefore do not run into problems after renaming the method in an IDE that can't find all its 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";
   }

...

When invoking a Bean you can instruct Camel to invoke a specific method by providing the method name:

Code Block

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

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

Code Block

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

...

If you want to pass in a null value, then you can explicit define this in the method option as shown below:

Code Block

   .to("bean:orderService?method=doSomething(null, true)")

...

Besides the message body, you can pass in the message headers as a java.util.Map:

Code Block

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

Code Block

   .bean(MyBean.class, "echo('World', 5)")

...

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

...

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:

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.