Apache Camel + Quartz Example | CodeUsingJava

Apache Camel + Spring + Quartz Example


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


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

Apache Camel Quartz Tutorial

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{

    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"

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

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<routeBuilder ref="routeBuilder" />
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");
        System.out.println("Application context started");
        try {
            Thread.sleep(6 * 60 * 1000);
        catch (InterruptedException e) {

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


Apache Camel Quartz Example