Monday, November 29, 2021

Spring Integration With Quartz Scheduler

This post shows how you can integrate Spring with Quartz Scheduler which is a tool for scheduling jobs.

Using Quartz scheduler

Quartz uses Job, JobDetail and Trigger objects to realize scheduling of all kinds of jobs. Following are some of the main classes/interfaces in Quartz Scheduler API.

  • Scheduler- This is the main interface of a Quartz Scheduler. A Scheduler maintains a registry of JobDetails and Triggers. Once registered, the Scheduler is responsible for executing jobs when their associated triggers fire (when their scheduled time arrives).
  • Job- Job is an interface which is to be implemented by classes which represent a 'job' to be performed. Job interface has a single method execute(JobExecutionContext context) which is called by the Scheduler when a Trigger fires that is associated with the Job.
  • JobDetail- Quartz does not store an actual instance of a Job class, but instead allows you to define an instance of one, through the use of a JobDetail interface.
  • JobDataMap- This class holds state information for Job instances. JobDataMap instances are stored once when the Job is added to a scheduler.
  • Trigger- Triggers are the 'mechanism' by which Jobs are scheduled. Triggers have a TriggerKey associated with them, which should uniquely identify them within a single Scheduler.

Spring integration with Quartz

Spring framework offers classes that simplify the usage of Quartz within Spring-based applications.

  • QuartzJobBean class- This abstract class is a simple implementation of the Quartz Job interface. It also applies the passed-in job data map as bean property values.
  • JobDetailFactoryBean- A Spring FactoryBean for creating a Quartz JobDetail instance, supporting bean-style usage for JobDetail configuration.

For example- Suppose you have a class EmailService for sending mails then it can be given as a property in a Job class than extends QuartzJobBean. The JobDetail instance can be configured using JobDetailFactoryBean where the job can be defined as a property along with JobDataMap.

public class MailReminderJob extends QuartzJobBean {

 private EmailService emailService;
 public void setEmailService(EmailService emailService) {
  this.emailService = emailService;
 protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
<bean name="emailReminderJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
  <property name="jobClass" value="org.netjs.service.MailReminderJob"/>
  <property name="jobDataAsMap">
      <entry key="emailService" value-ref="emailService"></entry>

Using MethodInvokingJobDetailFactoryBean

If you just need to invoke a method on a specific object you can use the MethodInvokingJobDetailFactoryBean to do that.

<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="exampleBusinessObject"/>
  <property name="targetMethod" value="doIt"/>

The above configuration will result in the doIt method being called on the exampleBusinessObject bean.

Configuring triggers

By now you have job and job details, you also need to configure triggers for scheduling the jobs.

Spring integration with Quartz offers two Quartz FactoryBean implementations with convenient defaults-

  • CronTriggerFactoryBean- Using this factory bean you can provide a cron expression.
  • SimpleTriggerFactoryBean- Using this factory bean you can provide properties for job scheduling like start time, repeat interval, start delay.

An example configuration for SimpleTriggerFactoryBean and CronTriggerFactoryBean-

<bean id="simpleTrigger" class=

    <property name="jobDetail" ref="jobDetail"/>
    <!-- 5 seconds -->
    <property name="startDelay" value="5000"/>
    <!-- repeat every 60 seconds -->
    <property name="repeatInterval" value="60000"/>
<bean id="cronTrigger" class=
    <property name="jobDetail" ref="exampleJob"/>
    <!-- run every morning at 8 AM -->
    <property name="cronExpression" value="0 0 8 * * ?"/>

Scheduling triggers

Triggers need to be scheduled. Spring offers a SchedulerFactoryBean that exposes triggers to be set as properties. SchedulerFactoryBean schedules the actual jobs with those triggers.

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
      <ref bean="cronTrigger"/>
      <ref bean="simpleTrigger"/>

That's all for this topic Spring Integration With Quartz Scheduler. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. Spring Job Scheduling Using TaskScheduler And @Scheduled Annotation
  2. Spring Thread Pooling Support Using TaskExecutor
  3. Spring MVC @RequestParam Annotation Example
  4. Spring Batch Processing With List of Objects in batchUpdate() Method
  5. Spring Component Scan Example

You may also like-

  1. Injecting Inner Bean in Spring
  2. Using p-namespace For Shorter XML configuration in Spring
  3. Spring depends-on Attribute
  4. @Conditional Annotation in Spring
  5. collect() Method And Collectors Class in Java Stream API
  6. Functional Interfaces in Java
  7. BigInteger in Java With Examples
  8. Apache Avro Format in Hadoop

No comments:

Post a Comment