Spring Quartz code snippets for beginners


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

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

 <!-- The 'Scheduler': quartz scheduler -->
 <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- list of triggers registered with the scheduler -->
  <property name="triggers">
    <!-- 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" />

     <!-- job schedule: every 5 minutes: CHANGE THIS -->
     <property name="cronExpression" value="0 0/30 * * * ?" />

    <!-- Trigger2: JobExample2 using SimpleTrigger -->
    <bean class="org.springframework.scheduling.quartz.SimpleTriggerBean">
     <property name="jobDetail">
       <!-- CHANGE THIS to the actual implementation class (see step 4)  -->
       <property name="jobClass" value="com.vineetmanohar.example.quartz.JobExample2" />
     <!-- wait 4 minutes after startup -->
     <property name="startDelay" value="240000" />
     <!-- every 1 min (60 sec) -->
     <property name="repeatInterval" value="60000" />


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.



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.

Related posts:

  1. Spring classpath scan breaks when migrating from JBoss4 to JBoss5
  2. JAXB code snippets for beginners
  3. EJB3 JPA error when migrating from JBoss version 4 to 5
  4. How to copy bean properties with a single line of code
  5. Maven FAQ and code snippets for beginners

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes