camel dynamic router EIP

Camel With Dynamic Router EIP with simple steps.

In this tutorial we learn about dynamic router EIP in camel.In dynamic router EIP , route is defined dynamically.Difference between routing slip and dynamic router is in routing slip we compute the slip up front while in dynamic router we compute route on the fly.Dynamic router can understand more with an example.So let us start


In our example we will send a message to queue and will call a bean where based on logic we will decide route where message will undergo..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 can use any latest version.

    <project xmlns="" xmlns:xsi=""
  • 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 {
    	public void configure() throws Exception {
    		from("direct:start").log("Sending ")
    				.dynamicRouter(bean(MyDynamicRouterBean.class, "route"))


    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 dynamicRouter where we have passed a bean MyDynamicRouterBean .When bean route method is invoked then camel invokes this method until it gets end of route .End of route it gets by null.

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

  • Copy below code in MyDynamicRouterBean.

    package com.preparationforinterview;
    import org.apache.camel.Exchange;
    import org.apache.camel.Header;
    public class MyDynamicRouterBean {
    	public String route(String body,
    			@Header(Exchange.SLIP_ENDPOINT) String previousRouter) {
    		return nextRoute(body, previousRouter);
    	private String nextRoute(String body, String previousRouter) {
    		String message = "I have prepared from"; 
    		if (previousRouter == null) {
    			return "mock://a";
    		} else if ("mock://b".equals(previousRouter)) {
    			return "language://simple:" + message;
    		} else if ("mock://a".equals(previousRouter)) {
    			return "mock://b";
    		} else {
    			return null;


    In above bean we have route method which will be invoked by camel and will continue invoke it until camel gets end of route.

    To test above dynamic route we will create one class

  • At the end we will have following structure

  • Copy below code in

    package com.preparationforinterview;
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.test.junit4.CamelTestSupport;
    import org.junit.Test;
    public class DynamicRouteTest extends CamelTestSupport {
    	protected RouteBuilder createRouteBuilder() throws Exception {
    		return new MyRoute();
    	public void testSplitterEIP() throws Exception {
    	 String output = "Hello, I am preparaing from";
    	 String message = "I have prepared from"; 


    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 expectedBodiesReceived and expectedMessageCount.After expectation has been set,we are sending message to direct:start queue.To Test execution with our result we fire assertion.template, we get from CamelTestSupport.

    To check the output right click on 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.DynamicRouteTest

Download source code

Camel With Dynamic Route 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