Bean method
Since Camel 1.3
The purpose of the Bean Language is to be able to implement an
Expression or Predicate using
a simple method on a bean. The bean name is resolved using a Registry, such as the
Spring ApplicationContext
, then a method is
invoked to evaluate the Expression or
Predicate. If no method name is provided then one
is chosen using the rules for Bean Binding;
using the type of the message body and using any annotations on the bean
methods.
The Bean Binding rules are used to bind the Message Exchange to the method parameters; so you can annotate the bean to extract headers or other expressions such as XPath or XQuery from the message.
Bean Language options
The Bean method language supports 5 options, which are listed below.
Name | Default | Java Type | Description |
---|---|---|---|
ref |
|
Reference to an existing bean (bean id) to lookup in the registry |
|
method |
|
Name of method to call |
|
beanType |
|
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 |
|
|
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. |
trim |
|
|
Whether to trim the value to remove leading and trailing whitespaces and line breaks |
Using Bean Expressions in Java
from("activemq:topic:OrdersTopic")
.filter().method("myBean", "isGoldCustomer")
.to("activemq:BigSpendersQueue");
Using Bean Expressions in Spring XML
<route>
<from uri="activemq:topic:OrdersTopic"/>
<filter>
<method ref="myBean" method="isGoldCustomer"/>
<to uri="activemq:BigSpendersQueue"/>
</filter>
</route>
**Bean Attribute Now Deprecated** The |
Writing the Expression Bean
The bean in the above examples is just any old Java Bean with a method
called isGoldCustomer()
that returns some object that is easily
converted to a boolean
value in this case, as its used as a
predicate.
Example:
public class MyBean {
public boolean isGoldCustomer(Exchange exchange) {
// ...
}
}
We can also use the Bean Integration annotations.
Example:
public boolean isGoldCustomer(String body) {...}
or
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 Language also supports invoking beans
that isn’t registered in the Registry. This is
usable for quickly to invoke a bean from Java DSL where you don’t need
to register the bean in the Registry such as the
Spring ApplicationContext
. Camel can instantiate
the bean and invoke the method if given a class or invoke an already
existing instance.
Example:
from("activemq:topic:OrdersTopic")
.filter().expression(BeanLanguage(MyBean.class, "isGoldCustomer"))
.to("activemq:BigSpendersQueue");
The 2nd parameter isGoldCustomer
is an optional parameter to
explicit set the method name to invoke. If not provided Camel will try
to invoke the most suitable method. If case of ambiguity Camel will
thrown an Exception. In these situations the 2nd parameter can solve
this problem. Also the code is more readable if the method name is
provided. The 1st parameter can also be an existing instance of a Bean
such as:
private MyBean my;
from("activemq:topic:OrdersTopic")
.filter().expression(BeanLanguage.bean(my, "isGoldCustomer"))
.to("activemq:BigSpendersQueue");
In Camel 2.2: you can avoid the BeanLanguage
and have it just as:
private MyBean my;
from("activemq:topic:OrdersTopic")
.filter().expression(bean(my, "isGoldCustomer"))
.to("activemq:BigSpendersQueue");
Which also can be done in a bit shorter and nice way:
private MyBean my;
from("activemq:topic:OrdersTopic")
.filter().method(my, "isGoldCustomer")
.to("activemq:BigSpendersQueue");
Other Examples
We have some test cases you can look at if it’ll help
-
MethodFilterTest is a JUnit test case showing the Java DSL use of the bean expression being used in a filter
-
aggregator.xml is a Spring XML test case for the Aggregator which uses a bean method call to test for the completion of the aggregation.
Spring Boot Auto-Configuration
When using bean with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 13 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
|
Whether to enable auto configuration of the bean component. This is enabled by default. |
Boolean |
||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |
|
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 delegate 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 then this depends on the delegated registry. |
BeanScope |
||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
|
Whether to enable auto configuration of the class component. This is enabled by default. |
Boolean |
||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |
|
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 delegate 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 then this depends on the delegated registry. |
BeanScope |
||
Whether to enable auto configuration of the bean language. This is enabled by default. |
Boolean |
||
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. |
Singleton |
String |
|
Whether to trim the value to remove leading and trailing whitespaces and line breaks. |
true |
Boolean |
|
Deprecated Use singleton option instead. |
true |
Boolean |
|
Deprecated Use singleton option instead. |
true |
Boolean |