August 25, 2009

Spring Quartz Example

« Spring Scheduler - ScheduledTimerTask | Main | Create new Maven Plugin »

Quartz represents an enterprise job scheduler. The mechanism of Quartz can be compared to Linux Cron jobs or JDK Timers. Spring integrates Quartz to offer scheduling and thread pooling features.

The following example describes two scheduling triggers/jobs. Triggers define the scheduling interval or the exact time of job execution. The SchedulerFactory references the triggers and initiates the scheduling.

<bean name="checkFTPForUpdates" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="" />

<bean name="checkDBForNewOrders" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass" value="" />

<bean id="cronFTPCheckTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="checkFTPForUpdates" />
    <!-- run at 14 PM every day -->
    <property name="cronExpression" value="0 0 14 * * ?" />

<bean id="cronOrdersCheckTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="checkDBForNewOrders" />
    <!-- run at 17 PM every day -->
    <property name="cronExpression" value="0 0 17 * * ?" />

<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
            <ref bean="cronFTPCheckTriggerBean" />
            <ref bean="cronOrdersCheckTriggerBean" />
The Job classes have the following structure.
public class CheckFTPJob extends QuartzJobBean {
  protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
      // check FTP
public class CheckOrderDBJob extends QuartzJobBean {
  protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
      // check DB
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 8:36 PM in Spring


[Trackback URL for this entry]

Comment: Himanshu at Mo, 11 Jan 10:30 AM

Nice article but I have a query. How the method in Job classes would be invoked? I mean how the JobExecutionContext ref would be obtained by calling class? I tried your example but now I'm stuck. Please do reply

Comment: Amulraj at Mi, 25 Mai 1:16 PM


Nice article,good job


Comment: Rabindra Singh at Do, 9 Jun 9:13 AM

I am highly impressed by this blog. This gives a lot of information about Quartz with Spring. I have also visited following link i.e. giving similarly good information about Quartz framework and its integration with both JSP, Servlet and also with Spring Framework. The links are:
Quartz integration with JSP,Servlet:

Quartz integration with Spring:

Comment: Stephan Scharff-Rahn at Di, 20 Sep 10:52 AM

This was my first arcticle I read at my start with Spring and Quartz. In addition I wanted to use a persistent job which I can configure in Spring to. The job can use a datasource or another services.

So I coded a GenericQuartzJobBean, that read the batch execution bean from the context. The generic job bean is serializable for the database:

public class GenericQuartzJobBean extends QuartzJobBean {

private static final Logger logger = Logger.getLogger(GenericQuartzJobBean.class);

private String beanNameOfBatch;

protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
BatchBean batchBean; // Eigenen Klasse / Interface für selbstdefinierte Batchabläufe
batchBean = getBatchFromContext(jobExecutionContext);;
} catch (Exception exception) {
logger.error("Could not read and start camel route job", exception);
throw new JobExecutionException(exception);

private BatchBean getBatchFromContext(JobExecutionContext jobExecutionContext) throws SchedulerException {
SchedulerContext schedulerContext = jobExecutionContext.getScheduler().getContext();
ApplicationContext applicationContext =
(ApplicationContext) schedulerContext.get("applicationContext");
return (BatchBean) applicationContext.getBean(beanNameOfBatch);

public void setBeanNameOfBatch(String beanNameOfBatch) {
this.beanNameOfBatch = beanNameOfBatch;

Your comment:

(not displayed)

Live Comment Preview: