Bean Method

Since Camel 1.3

The Bean language is used for calling a method on an existing Java bean.

Camel adapts to the method being called via Bean Binding. The binding process will for example automatic convert the message payload to the parameter of type of the first parameter in the method. The binding process has a lot more features so it is recommended to read the Bean Binding documentation for mor details.

Bean Method options

The Bean Method language supports 6 options, which are listed below.

Name Default Java Type Description

ref

String

Reference to an existing bean (bean id) to lookup in the registry.

method

String

Name of method to call.

beanType

String

Class name (fully qualified) of the bean to use Will lookup in registry and if there is a single instance of the same type, then the existing bean is used, otherwise a new bean is created (requires a default no-arg constructor).

scope

Singleton

Enum

Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while processing the request. The bean does not have to be thread-safe as the instance is only called from the same request. When using prototype scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope. So when using prototype scope then this depends on the bean registry implementation.

Enum values:

  • Singleton

  • Request

  • Prototype

resultType

String

Sets the class of the result type (type from output).

trim

true

Boolean

Whether to trim the value to remove leading and trailing whitespaces and line breaks.

Examples

In the given route below, we call a Java Bean Method with method, where "myBean" is the id of the bean to use (lookup from Registry), and "isGoldCustomer" is the name of the method to call.

from("activemq:topic:OrdersTopic")
  .filter().method("myBean", "isGoldCustomer")
    .to("activemq:BigSpendersQueue");
It is also possible to omit the method name, then Camel would have to choose the best suitable method to use; this process is a complex, so it is good practice to specify the method name,

And in XML DSL

<route>
  <from uri="activemq:topic:OrdersTopic"/>
  <filter>
    <method ref="myBean" method="isGoldCustomer"/>
    <to uri="activemq:BigSpendersQueue"/>
  </filter>
</route>

The bean could be implemented as follows:

public class MyBean {
  public boolean isGoldCustomer(Exchange exchange) {
     // ...
  }
}

How this method uses Exchange in the method signature. You would often not do that, and use non Camel types. For example by using String then Camel will automatically convert the message body to this type when calling the method:

public boolean isGoldCustomer(String body) {...}

Using Annotations for bean integration

You can also use the Bean Integration annotations, such as @Header, @Body etc

public boolean isGoldCustomer(@Header(name = "foo") Integer fooHeader) {...}

So you can bind parameters of the method to the Exchange, the Message or individual headers, properties, the body or other expressions.

Non-Registry Beans

The Bean Method Language also supports invoking beans that are not registered in the Registry.

Camel can instantiate the bean of a given type and invoke the method or invoke the method on an already existing instance.

from("activemq:topic:OrdersTopic")
  .filter().method(MyBean.class, "isGoldCustomer")
  .to("activemq:BigSpendersQueue");

The 1st parameter can also be an existing instance of a Bean such as:

private MyBean my = ...;

from("activemq:topic:OrdersTopic")
  .filter().method(my, "isGoldCustomer")
  .to("activemq:BigSpendersQueue");

Unresolved include directive in modules/languages/pages/bean-language.adoc - include::spring-boot:partial$starter.adoc[]