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 {

    @Override
    public void configure() throws Exception {
        from("timer://myTimer?period=2000")
                .setBody()
                .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() {
        System.out.println("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
        camel.start();

        // so run for 10 seconds
        Thread.sleep(10_000);

        // and then stop nicely
        camel.stop();
    }


}

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;

/**
 * Class to configure the Camel application.
 */
public class MyConfiguration {

    @BindToRegistry
    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: https://github.com/fmarchioni/masterspringboot/tree/master/camel/camel-bean

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials
Cookie Policy

Cookie Policy This website uses cookies that are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By accepting this OR scrolling this page OR continuing to browse, you agree to our privacy policy.

© 2021 Your Company. All Rights Reserved. Designed By JoomShaper

Please publish modules in offcanvas position.