Saturday, 1 August 2015

Apache Camel with spring framework



Before to start understanding to configure the camel with spring framework and you are new in camel so please go through the below post.

 

Configure Apache Camel with spring framework:

 

We will use the same example which we did in previous post. This demo example ask to use to enter something at command prompt and reply back to user to same string in capitalize letter.

We are going to create a java string maven project using eclipse Kepler.

1.       First we will add the maven dependency

       <dependencies>            
              <dependency>
                     <groupId>org.springframework</groupId>
                     <artifactId>spring-context</artifactId>
                     <version>${spring.version}</version>
              </dependency>
             
              <dependency>
                     <groupId>org.apache.camel</groupId>
                     <artifactId>camel-core</artifactId>
                     <version>2.13.0</version>
              </dependency>
              <dependency>
                     <groupId>org.apache.camel</groupId>
                     <artifactId>camel-spring</artifactId>
                     <version>2.13.0</version>
              </dependency>
           <dependency>
                     <groupId>org.apache.camel</groupId>
                     <artifactId>camel-stream</artifactId>
                     <version>2.13.0</version>
              </dependency>       
       </dependencies>

       <properties>
              <spring.version>4.0.1.RELEASE</spring.version>
       </properties>

2.       Create a spring configuration file

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd         
              http://camel.apache.org/schema/spring
              http://camel.apache.org/schema/spring/camel-spring.xsd">

       <camelContext xmlns="http://camel.apache.org/schema/spring">
              <route>
             
                     <from uri="stream:in?promptMessage=Enter something: " />
             
                     <transform>
                           <simple>${body.toUpperCase()}</simple>
                     </transform>
                     <to uri="stream:out" />
              </route>
       </camelContext>
</beans>

As we all are well familiar with spring configuration file so I just cover only apache camel context. 

We can see that I have added <camelContext> tag with in spring configuration file.

This is a camel context file where we can add route. In this example we have add a single route where <from> tag is start point of the route.

Each route have only single <from> tag and multiple <to> tag as mentioned below:

<camelContext xmlns="http://camel.apache.org/schema/spring">
              <route>
                     <from uri="stream:in?promptMessage=Enter something: " />
                     <to uri="transform" />
                     <to uri="changetojson" />
                     <to uri="stream:out" />
              </route>
             
              <route>
                     <from uri="stream:in?promptMessage=Enter something: " />
                     <to uri="transform" />
                     <to uri="changetoXML" />
                     <to uri="stream:out" />
              </route>
             
</camelContext>
3.   Write the java class 
   
public class MyFirstCamelExample {

       public static void main(String[] args) {

//First load the spring configuration file
              AbstractApplicationContext camelContext = new ClassPathXmlApplicationContext("firstcamelexample.xml");
             
//Start the camel context
              camelContext.start();
             
// Wait for few second to process the camel for message exchange between endpoints. If we don’t add this so camel immediately close the service.
             
              try {
                     Thread.sleep(60000);
              } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }

//Stop the camel context         
             camelContext.stop();
       }

}

Apache Camel: File Component:

 

Apache camel also provides a support for file operations. I will write it another blog post to cover file operation but this example just read the file from one location and write it to console.

       <camelContext xmlns="http://camel.apache.org/schema/spring">
              <route>
                     <!-- read input from the file  -->
                     <from uri="file://c:/test/jigar" />
                     <!-- Write it to console -->
                     <to uri="stream:out" />
              </route>
       </camelContext>

We need to add route in camel context. When any file will trigger at “c:/test/jigar” location, Apache camel file component will take that file and pass it to next instruction ( print  the file content at console)

Java Class:

package com.jigarnagar.camel.firstexample;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MyFirstCamelExample {

public static void main(String[] args) {               

AbstractApplicationContext camelContext = new ClassPathXmlApplicationContext("firstcamelexample.xml");
             
              camelContext.start();
             
              try {
                     Thread.sleep(60000);
              } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
             
              camelContext.stop();
       }

}

You can try this examples and let me know if you face any issue, we will try to solve the issue.

Happy to see your comment