Tuesday, November 27, 2018

Apache Camel + Spring + Quartz Integration

 Apache Camel + Spring + Quartz Integration 

enter image description here

Purpose of 'Apache Camel' is to route 'messages' from one 'system' to another one in the world. Apache camel uses different transport mechanisms for message routing.Apache Camel picks up messages using 'Camel based Component' of the 'from' system and drops them using the 'Camel based Component' of the 'to' system. A message may route to multiple systems, but everywhere they have to go through 'Camel based Components' to travel between 'Apache Camel's underlying transport mechanism' and the system.

Apache Camel is a lightweight integration framework which implements all Enterprise Integration patterns.You can easily integrate different applications using the required patterns.

You can use Java, Spring XML, Scala or Groovy. Almost every technology you can imagine is available, for example HTTP, FTP, JMS, EJB, JPA, RMI, JMS, JMX, LDAP, Netty etc.

Camel uses a Java Domain Specific Language or DSL for creating Enterprise Integration Patterns or Routes in a variety of domain-specific languages (DSL) as listed below.

Java DSL - A Java based DSL using the fluent builder style.

The story of Enterprise Integration Pattern resolves around these concepts :
Message, End Point, Producer, Consumer, Routing, Bus, Transform and Process.

  • many senders and many receivers
  • a dozen of protocols (ftp, http, jms, etc.)
  • many complex rules
    • Send a message A to Receivers A and B only
    • Send a message B to Receiver C as XML, but partly translate it, enrich it (add metadata) and IF condition X, then send it to Receiver D too, but as CSV.

  • translate between protocols
  • glue components together
  • define routes - what goes where
  • filter some things in some cases
We will learn how can we copy file from one location to other location using camel.The files will be copied from inputfolder to the outputfolder after delay of 3 minutes we have configured using quartz.

Create maven project

 



The pom.xml will be as follows-


<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>com.camel</groupId>
     <artifactId>CAMEL</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>jar</packaging>

     <name>CAMEL</name>
     <url>http://maven.apache.org</url>

     <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>

     <dependencies>
           <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
           </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-quartz</artifactId>
                <version>2.13.0</version>
           </dependency>

           <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.5</version>
           </dependency>

           <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.5</version>
           </dependency>

     </dependencies>
</project>

 

 Create the camel processor as follows-(MyProcessor.java)


package com.camel.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class MyProcessor implements Processor {

               public void process(Exchange exchange) throws Exception {
                              System.out.println("Executing camel processor");
                             
                             
               }

}




 Create the SimpleRouteBuilder class that extends RouteBuilder and configures the camel route. Here we will also define the quartz configuration using CronScheduledRoutePolicy (SimpleRouteBuilder.java)


package com.camel.route;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.routepolicy.quartz.CronScheduledRoutePolicy;

import com.camel.processor.MyProcessor;

public class SimpleRouteBuilder extends RouteBuilder {

     @Override
     public void configure() throws Exception {
           CronScheduledRoutePolicy startPolicy=new CronScheduledRoutePolicy();
           startPolicy.setRouteStartTime("0 0/3 * * * ?");
           from("file:C:/Practice/input?noop=true").
           log("Read from the input file").
           routePolicy(startPolicy).noAutoStartup().
           process(new MyProcessor()).
           to("file:C:/Practice/output?noop=true").
           log("Written to output file");
          
          
     }

}

 


Note:

Here we gave noop=true as parameter. It is optional parameter.Using that we can instruct camel to keep that input file as is.If we don't use this option then file will be moved, it means cut from input and paste in output


 Create the application context that calls the Java DSL RouteBuilder class using the routeBuilder
(applicationContext.xml)

<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">

     <bean id="routeBuilder" class="com.camel.route.SimpleRouteBuilder" />

     <camelContext xmlns="http://camel.apache.org/schema/spring">
           <routeBuilder ref="routeBuilder" />
     </camelContext>
</beans>
 



 Finally load the context file to start the Java DSL camel route.(MainApp.java)

package com.camel.main;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

     public static void main(String[] args) {
           AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
           ctx.start();
           System.out.println("Application context started");
          
           try {
                Thread.sleep(5 * 60 *1000);
           }catch (InterruptedException e) {
                e.printStackTrace();
           }
           ctx.stop();
           ctx.close();

     }

}

 
 Output:


log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Application context started
Executing camel processor
Executing camel processor
Executing camel processor
Executing camel processor
Executing camel processor
Executing camel processor
Executing camel processor
Executing camel processor
 






0 comments:

Post a Comment