Spring Boot + Apache Camel Quartz
Overview
In this article, we'll explore how to integrate Apache Camel Quartz with Spring Boot.
Apache Camel Tutorial :
- Apache Camel Overview and Architecture
- Install ActiveMQ And Start ActiveMQ Server on Windows
- Apache Camel with Spring Boot Simple Example
- Apache Camel Exception Handling Example
- Apache Camel ActiveMQ with Spring Boot Message Example
- Spring Boot + Apache Camel Quartz
- Apache Camel Interview Questions and Answers
What is a Apache Camel?
Apache Camel is an open-source integration framework for implementing various Enterprise Integration Patterns(EIPs), that allows you to quickly and easily connect different systems that consume or produce data.
Camel receives messages from some endpoint and sends it to another one. The messages can be processed or simply routed to different endpoint depending on transformation logic applied in Camel.
It allows integration simple by providing connectivity to a wide variety of transports and APIs.
For example, you can easily route JMS to JSON, JSON to JMS, HTTP to JMS, FTP to JMS, even HTTP
to HTTP, and connectivity to Microservices.
COMPONENTS
Component references are used to locate a component within an assembly. Apache Component References provides a variety of references that provide services for messaging, transmitting data, notifications, and a variety of other services that can not only resolve simple messaging and data transfer but also provide data security.
-
CORE COMPONENTS
Total Core Components: 25 in 22 JAR artifacts. Some of them are Bean, log,Class, REST API, Timer, XSLT etc.. -
NON-CORE COMPONENTS
There are total Non-Core Components: 320 in 247 JAR artifacts (1 deprecated). Some of them are activemq, amqp, atom, aws, aws lamda, kafka, aws mq, crypto etc..
Take a look at our suggested posts:
The Quartz component uses the Quartz Scheduler 2.x to deliver messages on a predetermined schedule. A different timer is represented by each endpoint (in Quartz terms, a Trigger and JobDetail).
A CronTrigger or a SimpleTrigger is used by the component. The component uses a simple trigger if no cron expression is specified. The quartz component uses the Camel group name if no groupName is specified.
In this tutorial, will implement Spring Boot + Apache Camel + Quartz scheduler with different use cases.
Let's get started on the project now.
Project Structure
- Create Spring Boot Maven project with Apache Camel dependencies from spring initializr
- Add below Apache Camel dependencies in pom.xml.
<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.techgeeknext</groupId> <artifactId>springboot-camel-quartz</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>${camel.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>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-quartz2</artifactId> <version>${camel.version}</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-stream</artifactId> <version>${camel.version}</version> </dependency> </dependencies> </project>
Building a Route
A route in Apache Camel is a collection of steps that Camel performs in order to consume and process a message.
Routes in Java are found within a RouteBuilder class, which has a configure() method where you can add your route code.
Create class which extends RouteBuilder and override the configure method.
In the below example, we are showing different use cases where in first case moving files from source folder to destination folder by triggering quartz scheduler at every 1 minute.
And in next use cases printing output at console on every 1 second using quartz scheduler.
package com.techgeeknext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
public class QuartzSchedulerRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
//CASE1 - Move files from one folder other
/*from("file:D://techgeeknext-camel-1?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("moving " + exchange.getIn().getBody());
}
}).to("file:D://techgeeknext-camel-2");
*/
//CASE2 - Print output at console
//repeatCount: How many times should the timer repeat?
//repeatInterval: The amount of time in milliseconds between repeated triggers.
from("quartz2://currentTimer?trigger.repeatInterval=1000&trigger.repeatCount=5")
.setBody().simple("TechGeekNext Quartz Example 1")
.to("stream:out");
//CASE3 - Print output at console on every 20 minutes
/*from("quartz2://Custom?cron=0/20+*+*+?+*+*")
.setBody().simple("TechGeekNext Quartz Example 3")
.to("stream:out");*/
}
}
What is Camel Context
The Camel context serves as both a runtime system and a loading container for all resources needed to complete the routing. It stores everything in one place so that the user can run the routing logic. When the context is launched, it also launches other components and endpoints, as well as the routing rules.
Create Camel Context
package com.techgeeknext;
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 SpringBootCamelQuartzApplication {
@Autowired
private CamelContext camelContext;
public static void main(String[] args) {
SpringApplication.run(SpringBootCamelQuartzApplication.class, args);
}
@Bean
public QuartzSchedulerRouteBuilder fileRouteBuilder() throws Exception {
QuartzSchedulerRouteBuilder routeBuilder = new QuartzSchedulerRouteBuilder();
camelContext.addRoutes(routeBuilder);
return routeBuilder;
}
}
Test the application
Now, start the application and you can notice the output for case 1 to move file from one folder other will be printed.
17:27:44.857 INFO 9716 --- [main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'DefaultQuartzScheduler-camel-1' initialized from an externally provided properties instance.
17:27:44.857 INFO 9716 --- [main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.0
17:27:44.893 INFO 9716 --- [main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: Endpoint[file://D://techgeeknext-camel-1?noop=true&recursive=true&scheduler=quartz2&scheduler.cron=0+0%2F1+*+1%2F1+*+%3F+*]
17:27:44.894 INFO 9716 --- [main] o.a.c.component.quartz2.QuartzComponent : Starting scheduler.
17:27:44.894 INFO 9716 --- [main] org.quartz.core.QuartzScheduler : Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED started.
17:27:44.894 INFO 9716 --- [main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started.
17:27:44.897 INFO 9716 --- [main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.17.3 (CamelContext: camel-1) started in 0.318 seconds
17:27:44.936 INFO 9716 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
17:27:44.940 INFO 9716 --- [main] c.t.SpringBootCamelQuartzApplication : Started SpringBootCamelQuartzApplication in 2.75 seconds (JVM running for 7.307
moving GenericFile[D:\techgeeknext-camel-1\test1.txt]
And for case 2, the output will be printed in console at every 1 second.
17:26:24.677 INFO 16288 --- [main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.0 created.
17:26:24.678 INFO 16288 --- [main] org.quartz.simpl.RAMJobStore : RAMJobStore initialized.
17:26:24.684 INFO 16288 --- [main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.0) 'DefaultQuartzScheduler-camel-1' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
17:26:24.684 INFO 16288 --- [main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'DefaultQuartzScheduler-camel-1' initialized from an externally provided properties instance.
17:26:24.685 INFO 16288 --- [main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.0
17:26:24.702 INFO 16288 --- [main] o.a.c.component.quartz2.QuartzEndpoint : Job Camel_camel-1.currentTimer (triggerType=SimpleTriggerImpl, jobClass=CamelJob) is scheduled. Next fire date is Tue Jul 27 17:26:24 IST 2024
17:26:24.751 INFO 16288 --- [main] o.a.camel.spring.SpringCamelContext : AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
17:26:24.751 INFO 16288 --- [main] o.a.camel.spring.SpringCamelContext : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
17:26:24.878 INFO 16288 --- [main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: Endpoint[quartz2://currentTimer?trigger.repeatCount=5&trigger.repeatInterval=1000]
17:26:24.879 INFO 16288 --- [main] o.a.c.component.quartz2.QuartzComponent : Starting scheduler.
17:26:24.879 INFO 16288 --- [main] org.quartz.core.QuartzScheduler : Scheduler DefaultQuartzScheduler-camel-1_$_NON_CLUSTERED started.
17:26:24.879 INFO 16288 --- [main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started.
17:26:24.880 INFO 16288 --- [main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.17.3 (CamelContext: camel-1) started in 0.368 seconds
17:26:24.916 INFO 16288 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
TechGeekNext Quartz Example 1
17:26:24.920 INFO 16288 --- [main] c.t.SpringBootCamelQuartzApplication : Started SpringBootCamelQuartzApplication in 4.152 seconds (JVM running for 10.177)
TechGeekNext Quartz Example 1
TechGeekNext Quartz Example 1
TechGeekNext Quartz Example 1
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot + Apache Camel Quartz Example