camel splitter EIP

Camel With Splitter EIP with simple steps.


In this tutorial we learn about splitter EIP in camel.In splitter EIP small messages are created and processed.

Example

In our example we will send a message to queue and camel will call a method in bean which will return a Iterator.Splitter will iterate on Iterator and for every iteration it will send a message.From test case we will validate our output.So let us start

  • 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 and click on Finish



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



  • 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-test</artifactId>
    			<version>2.5.0</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.8.1</version>
    			<scope>test</scope>
    		</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 MyRoute.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class MyRoute extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    		from("direct:start")
    				.log("Sending ")
    				.split().method(Department.class, "getEmployees")
    				.log("After split,split message is  ")
    				.to("mock:output");
    
    	}
    
    }
    

    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 route we are accepting message from in memory route and logging it to see the content of the message.After that we are calling split method.Splitter needs Expression which is evaluated when message arrives and a Iterator is created on which splitter iterates. In our example we are calling one bean method that returns a List.Splitter iterates on List and send every entity as single message to mock:output

  • Right click on package and create two more java class for our bean.In our case we have Department and other is Employee.

  • Copy below code in Employee.

  •  	
    package com.preparationforinterview;
    
    public class Employee {
    	private int employeeId;
    
    	public int getEmployeeId() {
    		return employeeId;
    	}
    
    	public void setEmployeeId(int employeeId) {
    		this.employeeId = employeeId;
    	}
    
    	@Override
    	public String toString() {
    		return "Employee [employeeId=" + employeeId + "]";
    	}
    	
    	
    }
    
  • Copy below code in Department.

  •  	
    package com.preparationforinterview;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Department {
    
    	public List$lt;Employee$gt; getEmployees(final int noOfEmployee){
    		
    		System.out.println("Number of Employee Information required " + noOfEmployee);
    		
    		List$lt;Employee$gt; employees = new ArrayList$lt;Employee$gt;();
    		
    		for(int i=1;i<=noOfEmployee;i++){
    			Employee employee = new Employee();
    			employee.setEmployeeId(i);
    			employees.add(employee);
    		}
    		
    		return employees;
    		
    	}
    	
    }
    

    To test above splitter we will create one class SplitterTest.java.



  • At the end we will have following structure


  • Copy below code in SplitterTest.java.

  •  	
    package com.preparationforinterview;
    
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.component.mock.MockEndpoint;
    import org.apache.camel.test.junit4.CamelTestSupport;
    import org.junit.Test;
    
    public class SplitterTest extends CamelTestSupport {
    	@Override
    	protected RouteBuilder createRouteBuilder() throws Exception {
    		return new MyRoute();
    	}
    
    	@Test
    	public void testSplitterEIP() throws Exception {
    		MockEndpoint mock = getMockEndpoint("mock:output");
    		mock.expectedMessageCount(6);;
    		template.sendBody("direct:start",6);
    		assertMockEndpointsSatisfied();
    
    
    	}
    }
    

    Explain

    To get camel test kit advantage we need to extend CamelTestSupport.CamelTestSupport class is wrapped on Junit.When we extend CamelTestSupport then we need to override one method createRouteBuilder.In that method create an object of route class.In our case we have done MyRoute.To test router we created method annotated with @Test. In that method we are getting mock endpoint and we have set expectation for example Expected expectedMessageCount.After expectation has been set,we are sending message to direct:start queue ,here we are sending 6 which will work as parameter in our department method bean.To Test execution with our result we fire assertion.template, we get from CamelTestSupport.

    To check the output right click on SplitterTest.java and run it.It will pass test case and we will get following output.


    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.SplitterTest

Download source code

Camel With Splitter EIP



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