Camel With Bean Injection Registry Example using Java DSL

Camel With Bean Injection Registry Example using Java DSL with simple steps.


In this example we will learn how can we inject bean into camel route through java dsl and will understand registry in camel.

  • In eclipse go to MyFirstCamelProject that we created in earlier page and right click on src/main/java to create package.New -> package



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



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



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

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



  • Create two folder one with name input and other as output.At the end we will get following structure



  • Create any file in input folder.I created text file with name Hello and added line
  • Employee whose name is developer is preparing for interview from preparationforinterview.com
  • Add following dependency in pom.xml file.you can use any latest version.

  •  	
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>MyFirstCamelProject</groupId>
    	<artifactId>MyFirstCamelProject</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<dependencies>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-core</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    	</dependencies>
    </project>
    
  • 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 Executor.

  •  
    package com.preparationforinterview.java.bean;
    
    import org.apache.camel.CamelContext;
    import org.apache.camel.impl.DefaultCamelContext;
    import org.apache.camel.impl.SimpleRegistry;
    
    public class Executor {
    	public static void main(String args[]) throws Exception {
    		SimpleRegistry registry = new SimpleRegistry();
    		registry.put("employeebean", new Employee());
    		CamelContext ctx = new DefaultCamelContext(registry);
    
    		ctx.addRoutes(new BeanRouter());
    		ctx.start();
    		Thread.sleep(1000000);
    		ctx.stop();
    	}
    }
    
    

    Explain

    When camel works wit bean then to find any bean it first looks bean name in camel registry and then camel registry looks into real registry which is implementation of camel registry.With camel package we get some implementation by default for example JndiRegistry,ApplicationContextRegistry,SimpleRegistry.By default camel uses JndiRegistry .When we use spring then ApplicationContextRegistry is used.So we need to provide bean name entry in registry,here we are using SimpleRegistry.

  • Copy below code in Employee which is our bean class

  •  
    package com.preparationforinterview.spring.bean;
    
    public class Employee {
    
    	public String employeeDetails(final String inputDetails) {
    		return "Hello employee details are as follows. " + inputDetails;
    
    	}
    
    }
    
  • Write lines in BeanRouter.java

  •  	
    package com.preparationforinterview.java.bean;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class BeanRouter extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		
    		from("file:input?noop=true").beanRef("employeebean").to(
    				"file:output");
    	}
    
    }
    

    Explain

    BeanRouter is our route class.We created a employee bean and in camelcontext we have added our registry with bean name employeebean.We will refer that same bean name.If we have only single public method then we don't need to give method name. Only beanRef("employeebean") is sufficient. When we start the route it will take input from input folder and with that value will call to bean method.As bean method employeeDetails return String. that will be sent to output folder.If we open file in output folder,we will get following output.

    Hello employee details are as follows. Employee whose name is developer is preparing for interview from preparationforinterview.com
    To check the output right click on Executor.java and run it. It will copy file in output folder from input folder
    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.java.bean.Executor

Download source code

Camel With Bean Injection Example using Java DSL



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