camel Bean Parameter Binding Example

Camel Bean Parameter Binding Example With Simple Steps.


In our example we will learn how can we pass parameter to bean which have method accepting more than one parameter.If bean has a method that accepts only single parameter then message body is assigned to that parameter provided that are compatible but problem occurs when bean method accepts two parameter because in that case message body is always assigned to first parameter and second will always be null.By using camel annotation we can solve it.

Note: Above explanation is not for built in type like Exchange, Message, CamelContext etc because camel assign that parameter value automatically.

As a broker we are using ActiveMQ.So we have assumed you have ActiveMQ in your local machine.

  • In eclipse create a maven project MyFirstCamelJMSProject as we created in earlier page and right click on src/main/java to create package. New -> package



  • Give any name for example com.preparationforinterview.jms and click on Finish



  • Right click on package and create a java class for example JmsInCamelExample.



  • Right click on package and create one more java class for example JmsExecutor.



  • Right click on package and create one more java class for example Employee.

  • Add following dependency in pom.xml file.you can use any latest version.

  •  	
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-core</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-jms</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.activemq</groupId>
    			<artifactId>activemq-core</artifactId>
    			<version>5.3.2</version>
    		</dependency>
    
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Copy below code in Employee which is our bean class

  •  
    package com.preparationforinterview.jms;
    
    import org.apache.camel.Body;
    import org.apache.camel.Header;
    
    public class Employee {
    	public String employeeDetails(@Header("department") String department, @Body final String inputDetails) {
    		return "Hello employee details are as follows. " + inputDetails
    				+ " departmentId:" + department;
    
    	}
    
    }
    

    Explain

    Here we created our bean class.In our bean we have one method employeeDetails which accepts two parameter.Intentionally we are providing body as second parameter so that we can check our annotation are working.

  • Copy below code in JmsInCamelExample.

  •  		
    package com.preparationforinterview.jms;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class JmsInCamelExample extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    
    		try {
    			from("jms:queue:input").
    					bean(Employee.class)
    					.to("jms:queue:preparationforinterview");
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}
    
    	}
    }
    

    Explain

    To create a route in camel we need to extend RouteBuilder class and override configure method.In configure method we define our route.In above example we can read it as from input queue take message and call to bean method. Send returned object to queue "preparationforinterview".Here jms: is called endpoint.

  • Copy below code in JmsExecutor.

  •  	
    package com.preparationforinterview.jms;
    
    import javax.jms.ConnectionFactory;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.apache.camel.CamelContext;
    import org.apache.camel.component.jms.JmsComponent;
    import org.apache.camel.impl.DefaultCamelContext;
    
    public class JmsExecutor {
    	public static void main(String args[]) throws Exception {
    		 CamelContext context = new DefaultCamelContext();
    	 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    		 context.addComponent("jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
    		 context.addRoutes(new JmsInCamelExample());
    		 context.start();
    		 Thread.sleep(1000);
    		 context.stop();
    		 }
    
    }
    

    Explain

    First we need to create CamelContext. It has many implementation but we are using DefaultCamelContext.Camel provides ConnectionFactory but to which provider we need to connect we have to give that here we are using ActiveMQ so give ActiveMQConnectionFactory and add it to camel JmsComponent and link it to "jms" name ,you can use any name but whatever you will give, use same in JmsInCamelExample router class.After that add route in camel context.Its like registry of route in CamelContext, till that part we have just configured route in CamelContext but until and unless we start CamelContext it will not execute route.We kept Thread.sleep(1000) so that camel gets enough time to copy file from input to output folder.

    To check the output right click on JmsExecutor.java and run it.It will take message from input queue and send output to preparationforinterview queue.Note that you start your activemq first.When we start our application it will show as below.


    For message sending i am using Jmeter there i will set header as well. As body i set as name:preparationforinterview and header as teacher.



    When we send message to input queue then after processing we will get message to output queue preparationforinterview.It will show as below.




    To run the program from command prompt, open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.JmsExecutor

Download source code

Camel Bean Parameter Binding Example



Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019 PreparationForInterview.com