Spring Boot + Apache Camel + Quartz Example | CodeUsingJava








Spring Boot + Apache Camel + Quartz Example


Why use Apache Camel?

Suppose our application is receiving data  from multiple sources like queues, file system. This data will need to be transformed and then sent to maybe to a database.
Apache Camel + Spring Boot System
  • This will need a lot of custom code for reading the data from multiple sources, transformation and storing.
  • Also if in future more sources from which data is to be read are added it will be a lot of changes.
Apache camel provides us with a number of out of the box components. Using these components we create endpoints with which a system can interact with other external systems. For example using an ActiveMQ component we expose an ActiveMQ endpoint for interaction with external system. If tomorrow we need to read from file system then the file component can be added. So we do not need to write custom/boilerplate code for external interaction.Also Apache camel makes data transformation and routing very simple. When we write Java Code, we follow Design Patterns for better coding practices. Some of the benefits of using design patterns are: Design Patterns are already defined and provides industry standard approach to solve a recurring problem, so it saves time if we sensibly use the design pattern. Similarly for Enterprise Application Integration we use Enterprise Integration Patterns as these are proven solutions for recurring problems during application integration.

Overview

In this tutorial we will be implementing a Spring Boot + Apache Camel Project. We will be configuring an Apache Camel route for copying files from one folder to another. Also we will be triggering this route using Quartz.
Apache Camel + Spring Boot + Quartz Tutorial

Video Tutorial

Table Of Contents :


Technology Stack

We will be making use of-
  • Java 1.8
  • Camel Spring Boot Starter - 2.17.0
  • Camel Quartz - 2.17.0
  • Maven

Implementation

Spring Boot + Apache Camel + Quartz implementation

This will be the standard directory layout for maven project structure-
Apache Camel + Spring Boot + Quartz 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>springboot-camel</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.source.version>1.8</project.source.version>
		<project.target.version>1.8</project.target.version>
		<camel.version>2.17.3</camel.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring-boot</artifactId>
			<version></version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version></version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version></version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-quartz2</artifactId>
			<version></version>
		</dependency>

	</dependencies>
</project>
First we will create a class named FileRouteBuilder which extends the RouteBuilder class. In the configure method we define the apache camel route. Our configured route -
  • Gets triggered using the quartz cron expression which we have defined as gets triggered after 1 minute.
  • Move files from folder1 to folder2

package com.codeusingjava;

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

public class FileRouteBuilder extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:R://folder1?recursive=true&noop=true&scheduler=quartz2&scheduler.cron=0 0/1 * 1/1 * ? *")
				.process(new Processor() {

					public void process(Exchange exchange) throws Exception {
						System.out.println("transferring " + exchange.getIn().getBody());

					}
				}).to("file:R://folder2");
	}
}
Next we create the Spring Boot class with the SpringBootApplication annotation. In this class we create bean for the FileRouteBuilder class. We make use of the CamelContext provided by Apache Camel for loading the routes defined in the FileRouteBuilder class.

package com.codeusingjava;

import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootHelloWorldApplication {

	@Autowired
	private CamelContext camelContext;

	public static void main(String[] args) {
		SpringApplication.run(SpringBootHelloWorldApplication.class, args);
	}

	@Bean
	public FileRouteBuilder fileRouteBuilder() throws Exception {
		FileRouteBuilder routeBuilder = new FileRouteBuilder();
		camelContext.addRoutes(routeBuilder);
		return routeBuilder;
	}
}

What is CamelContext?

We cannot run our Camel Application only using the route. We also need the camel context which acts as a runtime system that runs and manages the routes. It is responsible for all managing all aspects of a route.
Apache Camel Maven Project
Start the Spring Boot application. Quartz will trigger the route after 1 minute.
Apache Camel + Spring Boot + Quartz Output

Downloads-

Spring Boot + Apache Camel + Quartz Example