Apache Camel + Quartz Example | CodeUsingJava








Apache Camel + Spring + Quartz Example


Overview

This section describes about how to develop a simple Apache Camel Quartz Example web application that highlights some of camel-quartz key features. We use Maven to build this project. In this example we will develop a application that will copy the files from one directory to different output directory, after a scheduled time.

Table Of Contents :


Technology Stack

We will be making use of-
  • Java 1.8
  • Apache Camel 3.0.0-M2
  • Maven
  • Camel Quartz 3.0.0-RC2
between

Implementation

Apache Camel Quartz implementation

This will be the standard directory layout for maven project structure-
Apache Camel Maven Project
We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.

<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.codeusingjava</groupId>
	<artifactId>camel-quartz-codeusingjava</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>3.0.0-M2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>3.0.0-M2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>camel-quartz</artifactId>
			<version>3.0.0-RC2</version>
		</dependency>
	</dependencies>
</project>

Apache Camel Quartz Tutorial

between
First we will create a class QuartzProcessor which will implement Processor interface from org.apache.camel package. So our QuartzProcessor.java will be as follows-

package com.processor;

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

public class QuartzProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
    	System.out.println("Executing camel processor");
    }
}
So in the above code as you can see we have implemented abstract process method which is present in Processor Interface. An Exchange is the message container holding the information during the entire routing of a Message received by a Consumer.

Apache Camel Quartz Example


Next we will create the QuartzRouteBuilder class that extends RouteBuilder and configures the camel route. The Java DSL is available by extending the RouteBuilder class, and implement the configure method. Here we will also define the quartz configuration using CronScheduledRoutePolicy. We have set timer in (setRouteStartTime) for every one minute.

package com.route;

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

import com.processor.QuartzProcessor;

public class QuartzRouteBuilder extends RouteBuilder{

	@Override
    public void configure() throws Exception {
    	CronScheduledRoutePolicy myRoutePolicy = new CronScheduledRoutePolicy();
    	myRoutePolicy.setRouteStartTime("0 0/1 * * * ?");

    	from("").routePolicy(myRoutePolicy).noAutoStartup().process(new QuartzProcessor()).to("");
	}
}
Create the application context that calls the Java DSL RouteBuilder class using the routeBuilder

<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.route.QuartzRouteBuilder" />

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<routeBuilder ref="routeBuilder" />
	</camelContext>
</beans>
between
We use Java Code to define our RouteBuilder implementations. These can be defined as beans in spring and then referenced in your camel context. Finally load the context file QuartzMainApp to start the Java camel route.


package com.quartz.main;

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

public class QuartzMainApp {

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

}
The files will be copied from inputfolder to the outputfolder after delay of 1 minutes we have configured using quartz.

Downloads-

Apache Camel Quartz Example