Camel Bean example

The Bean component in Camel binds beans to Camel message exchanges. Let’s see a Camel Bean example.

Camel Bean Component example

The purpose of this example is to show you can call a method of a Camel Bean Component :

public class MyRouteBuilder extends RouteBuilder {
  public void configure() throws Exception {
        .simple("Hello World Camel fired at:")
        .bean(new MyBean(), "setTime(${body})")
        .bean(new MyBean(), "done()");

As you can see, we are calling the bean registered with MyBean passing the body of the Camel route (in the first case) and without passing any parameter (second case).

public class MyBean {
  public void setTime(String s) {
    System.out.println(s + new java.util.Date());

  public void done() {

You can run the example with the following Camel main:

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class MainApp {
  public static void main(String[] args) throws Exception {

    // create a CamelContext         

    CamelContext camel = new DefaultCamelContext();
    camel.addRoutes(new MyRouteBuilder());

    // start is not blocking         


    // so run for 10 seconds         

    Thread.sleep(10 _000);

    // and then stop nicely         


You will see the Bean invoked every 2 seconds, until we stop the Camel Context:

Hello World Camel fired at:Sun Feb 07 18:41:06 CET 2021 Done Hello World Camel fired at:Sun Feb 07 18:41:08 CET 2021 Done  

Calling Camel Bean dynamically

In our example we have invoked the Camel Bean statically. If we were to include the Bean itself as parameter, we can use the generic .bean element as in this example:

public void configure() throws Exception {
  from("quartz:foo?cron={{myCron}}").bean("myBean", "hello").log("${body}").bean("myBean", "bye").log("${body}");

When using the generic bean component, you need to bind the Camel Bean in the registry. The simplest way to do it is via a Configuration class which contains the @org.apache.camel.BindToRegistry annotation. Example:

import org.apache.camel.BindToRegistry;
import org.apache.camel.PropertyInject;
public class MyConfiguration {
  public MyBean myBean() {

    // this will create an instance of this bean with the name of the method (eg myBean)         

    return new MyBean();
  public void configure() {
    // this method is optional and can be removed if no additional configuration is needed.     


We have just covered how to use the Camel Bean Component with a simple example

Source code available at: