Loading
 
What's Next: View All Tutorial →

How to call simple java funciton using bean component in Apache Camel

In this article you will learn how to call simple java funciton using bean component in Apache Camel Let's say, we have two function in Calculator.java class   public class Calculator { public int add10(int a) { Syst...

Published Date: 04/05/2015  Last Modified Date: 04/05/2015    New Post



In this article you will learn how to call simple java funciton using bean component in Apache Camel


Let's say, we have two function in Calculator.java class
 
public class Calculator {

    public int add10(int a) {
        System.out.println("Method Signature : int add(int a)");
        return a + 10;
    }

    public int add(int a, Integer b) {
        System.out.println("Method Signature : int a, Integer b");
        return a + b;
    }
}

Now we will call this in the Apache camel using the bean component

Camel Bean Component Example

package com.mycompany.camel;

import java.util.HashMap;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelProcessor {

    public static void main(String args[]) throws Exception {

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                from("direct:invokeaddFuntion").bean(Calculator.class, "add10").end();
                from("direct:invokeAddFuntionTwoParam").bean(Calculator.class, "add", true).end();
                from("direct:invokeAddFuntionTwoParamWithHeader").bean(Calculator.class, "add(${header.a},${header.b})").end();
                from("direct:invokeaddFuntionWithHarcode").bean(Calculator.class, "add(2,4)").end();
            }
        });
        context.start();
        Thread.sleep(9000);

        ProducerTemplate producer = context.createProducerTemplate();

        Object res = producer.sendBody("direct:invokeaddFuntion", ExchangePattern.InOut, 1);
        System.out.println(res);

        Object arr[] = new Object[2];
        arr[0] = 1;
        arr[1] = 3;
        Object b = producer.sendBody("direct:invokeAddFuntionTwoParam", ExchangePattern.InOut, arr);
        System.out.println(b);

        Map paramMap = new HashMap();
        paramMap.put("a", 1);
        paramMap.put("b", 1);

        Object result = producer.sendBodyAndHeaders("direct:invokeAddFuntionTwoParamWithHeader", ExchangePattern.InOut, null, paramMap);
        System.out.println(result);

        Object harcoderesult = producer.sendBody("direct:invokeaddFuntionWithHarcode", ExchangePattern.InOut, null);
        System.out.println(harcoderesult);
    }

}


Here we have created four router with different endpoint,


We call each end point with the producerTemplate.sendBody function with ExchangePattern.InOut
 

Output:

[                          main] DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) is starting
[                          main] ManagedManagementStrategy      INFO  JMX is enabled
[                          main] DefaultTypeConverter           INFO  Loaded 197 type converters
[                          main] DefaultCamelContext            INFO  AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
[                          main] DefaultCamelContext            INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
[                          main] DefaultCamelContext            INFO  Route: route1 started and consuming from: Endpoint[direct://invokeaddFuntion]
[                          main] DefaultCamelContext            INFO  Route: route2 started and consuming from: Endpoint[direct://invokeAddFuntionTwoParam]
[                          main] DefaultCamelContext            INFO  Route: route3 started and consuming from: Endpoint[direct://invokeAddFuntionTwoParamWithHeader]
[                          main] DefaultCamelContext            INFO  Route: route4 started and consuming from: Endpoint[direct://invokeaddFuntionWithHarcode]
[                          main] DefaultCamelContext            INFO  Total 4 routes, of which 4 is started.
[                          main] DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) started in 0.893 seconds
Method Signature : int add(int a)
11
Method Signature : int a, Integer b
4
Method Signature : int a, Integer b
2
Method Signature : int a, Integer b
6


 
Awaiting for Administrator approval





Tags: Basic bean component

← Back to list


Related Post




×