Bean/Class

Learn how to use the Bean/Class component.

Create a jar

Example of code:

package testpackage;
public class TestBean {
    private String m_value;

    public static void testWithBean() {
        System.out.println("Call testWithBean method.");
    }
   
    public void testWithTo() {
        System.out.println("Call testWithTo method.");
    }

    public void seeValueParameter() {
        System.out.println("Call seeValueParameter method with, from connector conf, value:" + m_value);
    }

    public void methodWithParam(String value) {
        System.out.println("Call methodWithParam method with param:" + value);
    }

    public void test() {
        System.out.println("Call test");
    }

    public String getValue() {
        return m_value;
    }
    public void setValue(String value) {
        m_value = value;
    }
}

With this, create a jar (test.jar here).

Update into library

Upload jar to Data Integration > Libraries:

Create a connector

Create a connector in Data Integration > Connectors:

As you can see, bean properties with public accessor are visible and can be set.

You can use data integration properties in order to define String or numerical values, to do so you need to type {{PROPERTY_NAME}} in the value input.

Use it in a route

Create a new route in Data Integration > Routes:

<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="timer://readloop?fixedRate=true&amp;period=10000"/>

        <!-- with bean component directly-->
        <bean ref="test" method="testWithBean"/>

        <!-- with bean component in a to -->
        <to uri="bean:test?method=testWithTo"/>

        <!-- see a parameter set in Connectors part-->
        <to uri="bean:test?method=seeValueParameter"/>

        <!-- method with a parameter-->
        <setBody>
            <simple>my great body</simple>
        </setBody>
        <convertBodyTo type="java.lang.String"/>
        <to uri="bean:test?method=methodWithParam"/>

        <!-- with class, instantiate the bean and call the method. Cannot use static method-->
        <setHeader headerName="changedValue">
            <simple>my great param</simple>
        </setHeader>
        <to uri="class:java.lang.String?method=contains(${headers.changedValue})"/>
        <log message="${body}"/><!-- trace false here-->

        <setBody>
            <simple>10000000</simple>
        </setBody>


        <!--call static method of a class with parameter and set it to a header param-->
        <setHeader headerName="test">
            <method beanType="org.joda.time.Duration" method="standardDays(${body})"/>
        </setHeader>
        <log message="${headers.test}"/>

        <!--same as before but the body is the default param-->
        <bean beanType="org.joda.time.Duration" method="standardDays"/>
        <log message="${body}"/>

        <!--call static method of a class without params-->
        <bean beanType="com.systar.tau.util.time.TimeUtil" method="createInstantAtNow"/>
        <log message="${body}"/>
     </route>
</routes>

Use a bean without creating a connector

In some cases, instantiating a bean using a connector is not needed. You may want to directly call  a bean static method of library:

  1. Link your context to the library you just created. For information, see How to use libraries in a context route
  2. Create a context:
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://camel.apache.org/schema/spring" xmlns:u="http://www.systar.com/aluminium/camel-util">
    <route>
        <from uri="timer:foo?repeatCount=1"/>
 
        <!-- call static method of a class -->
        <bean beanType="testpackage.TestBean" method="testWithBean"/>
 
        <!-- instantiate the bean and call the method in scripting language -->
        <setBody>
            <groovy>
                <![CDATA[
                   def test = new testpackage.TestBean();
                   test.setValue("It works")
                   test
                ]]>
            </groovy>
        </setBody>
        <log message="${body.value}"/>
    </route>
</routes>



Related Links