Spring Quartz Code Snippets for Beginners
Join the DZone community and get the full member experience.
Join For FreeSome applications need to run periodic jobs in the background. On Linux/Unix systems, background processes can be run using the “cronjob” scheduling service. In Java web applications, you can add scheduling to any app in 4 simple steps using Spring and Quartz. This article outlines the steps needed for scheduling, with code snippets required for each step.
Step 1: web.xml
To initialize Spring in your webapp, add this snippet to your web.xml file. If you are already using Spring, you probably already have this in your web.xml.
<!-- Initialize Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Step 2: applicationContext.xml snippet
There are 3 concepts when configuring your scheduled jobs:
1) The Job: the java code that you want to run in the background periodically
2) A Trigger: a trigger adds scheduling information to your job. There are different types of triggers (e.g. SimpleTrigger, CronTrigger). They differ in the way you specify scheduling information. For example, CronTrigger takes a cronexpression, whereas SimpleTrigger takes a repeatInterval.
3) The Scheduler: this is the actual service which runs in the background and invokes ‘triggers’. You can register multiple triggers with this service.
Add this snippet to your Spring xml file.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- The 'Scheduler': quartz scheduler --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- list of triggers registered with the scheduler --> <property name="triggers"> <list> <!-- Trigger1: JobExample using CronTrigger --> <bean id="jobDetailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <!-- job class --> <property name="jobDetail"> <bean class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- CHANGE THIS to the actual implementation class (see step 4) --> <property name="jobClass" value="com.vineetmanohar.example.quartz.JobExample" /> </bean> </property> <!-- job schedule: every 5 minutes: CHANGE THIS --> <property name="cronExpression" value="0 0/30 * * * ?" /> </bean> <!-- Trigger2: JobExample2 using SimpleTrigger --> <bean class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"> <bean> <!-- CHANGE THIS to the actual implementation class (see step 4) --> <property name="jobClass" value="com.vineetmanohar.example.quartz.JobExample2" /> </bean> </property> <!-- wait 4 minutes after startup --> <property name="startDelay" value="240000" /> <!-- every 1 min (60 sec) --> <property name="repeatInterval" value="60000" /> </bean> </list> </property> </bean> </beans>
Step 3: pom.xml snippet
If you are using Maven, add this to your pom.xml to add Spring and Quartz dependency to your project.
<project> <dependencies> <dependency> <groupId>org.opensymphony.quartz</groupId> <artifactId>quartz</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
Step 4: Java code snippet
Finally, write the actual implementation. Make sure that job class you specified in step 2 points to this file.
package com.vineetmanohar.example.quartz.JobExample; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class JobExample extends QuartzJobBean { protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException { System.out.println("Job invoked at : " + new java.util.Date()); // TODO: implement the job here } }
Cron expression
The format of cron expression is <Seconds> <Minutes> <Hours> <Day-of-Month> <Month> <Day-of-Week> <Year (optional field)>. For example:
Every 30 minutes
0 0/30 * * * ?
See full reference documentation here.
From http://www.vineetmanohar.com/2010/10/spring-quartz-code-snippets-for-beginners
Opinions expressed by DZone contributors are their own.
Comments