Quartz Spring Integration Example

Quartz scheduler is popular java open source to schedule jobs. It is best fit for standalone, medium or large applications. Its also very famous and mature open source tools which can be use for any java applications without much worrying. It provide many functionality and here we will integrate with spring and schedule our job using Cron expression.

Note: If you are new to Cron please go through this CronMaker web site where you could build validate your cron expression.

Below are needed:

  • Eclipse 4.0 or above
  • JDK 1.8
  • Quartz version 2.1.7
  • Maven 3.2
  • Below is final project structure:

Quartz Spring Integration Example

  • spring-context.xml: This is main configuration where we will create beans and include cron expression to schedule our job:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

	<context:annotation-config />
	
	<context:component-scan base-package="com.javahonk" />

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>Cron-Schedule.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean>

	<bean id="sampleJob" class="com.javahonk.SampleJob" />

	<bean id="cronQuartzJobScheduler" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="sampleJob" />
		<property name="targetMethod" value="executeScheduleJob" />
		<property name="arguments">		
			<list>
				<value>JavaHonk1</value>
				<value>JavaHonk2</value>
			</list>
		</property>
	</bean>

	<bean id="cronQuartzJobSchedulerCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="cronQuartzJobScheduler" />
		<property name="cronExpression" value="${cron.schedule.time}" />
	</bean>

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="cronQuartzJobSchedulerCronTrigger" />
			</list>
		</property>
		
		<!-- You can control number of thread you want to run from below -->
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
				<prop key="org.quartz.threadPool.threadCount">1</prop>
			</props>
		</property>
	</bean>

</beans>
  • Cron-Schedule.properties: I have many exprssion for use but for this tutorial using “cron.schedule.time= 0/10 * * ? * MON-SUN *” which will run our schedule job every 10 seconds.
#Cron scheduler run every minute
cron.schedule.time= 0/10 * * ? * MON-SUN *

#Run job every minute: 0 0/1 * 1/1 * ? *
#Run job everyday at 16:00 PM: 0 00 18 1/1 * ? *
#Run job everyday at multiple time start from 16:01,16:02... 21:01,21:02 etc..: 0 00,01,02,03 18,19,20,21 1/1 * ? *
#Run job every 15 minute: 0 0/15 * 1/1 * ? *
  • SampleJob.java: Java class where method “executeScheduleJob(String value1, String value2)” taking two parameter, both are String.
package com.javahonk;

import java.time.LocalDateTime;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobExecutionException;

public class SampleJob {
	
	private static final Logger LOGGER = LogManager.getLogger(SampleJob.class.getName());
	
	public void executeScheduleJob(String value1, String value2) throws JobExecutionException {
		
		LOGGER.info("Received two values: "+value1+"  Value2: "+value2);
		LOGGER.info("Cron Quartz Job started at time: {}", LocalDateTime.now());		
		
	}

}
  • SampleJobTestApp: Test class to load Spring context file and run the application:
package com.javahonk;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Java Honk
 *
 */
public class SampleJobTestApp {
	
	private static final Logger LOGGER = LogManager.getLogger(SampleJobTestApp.class.getName());
	
	public static void main(String[] args) {
		
		LOGGER.info("Application starting...");	
		
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
		
		registerShutdownHook(context);
	}
	
	private static void registerShutdownHook(ApplicationContext context) {
		
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

			@Override
			public void run() {
				LOGGER.info("LifeCycleManagerApp Exiting.");
				((AbstractApplicationContext) context).close();
			}
		}));
	}
	
}

Reference:

Download project source code: SpringQuartzIntegration

2 thoughts on “Quartz Spring Integration Example”

Leave a Reply

Your email address will not be published. Required fields are marked *