Using Quartz Scheduler with Spring framework

In the previous tutorial, we learned about Quartz Scheduler. Spring framework also has a wrapper for Quartz Scheduler, so if your application uses Spring framework and needs to use Quartz Scheduler, then read this tutorial. I will show you how to use Quartz Scheduler with the Spring framework in this tutorial.

First, I will create a new Maven project as an example:

Using Quartz Scheduler with Spring framework

To use Quartz with the Spring framework, you need to declare Spring Context Support, Spring Transaction, and Quartz Scheduler dependencies as follows:

We also need to declare to use the concepts of Job, Trigger, and Scheduler similar to Quartz Scheduler standalone, in Spring.

The first is about Job.

We have two ways to declare a Quartz Job with Spring, including:

  • Declaring a class with a method of this class will serve to define the task that we need to run.
  • Secondly, we will create a new class that extends QuartzJobBean of Spring Quartz.

I will create a new configuration file of Spring spring.xml in the directory src/main/resources:

Using Quartz Scheduler with Spring framework

as follows:

For the first way, you need to create a class with any method, my example is as follows:

then, you declare a Quartz Job in the spring.xml file using the MethodInvokingJobDetailFactoryBean object as follows:

with the targetObject property of MethodInvokingJobDetailFactoryBean will refer to our class’s “hello” bean in the Spring container and the targetMethod which will have the value as the method name in the Hello class, will execute the task.

For the second way, we need to create a new class that extends the abstract class QuartzJobBean (if you see the code of QuartzJobBean, you will see this class implements the Job interface of Quartz Scheduler):

As you can see, here we need to implement the executeInternal() method to execute the task.

Then, declare this class in the Spring container using the JobDetailFactoryBean object as follows:

If you need to transfer data into Hello class, you can declare using the Setter method to set this data to Hello class, then use the jobDataMap property of the JobDetailFactoryBean object to ingest this data.

For example, I have a Student class as follows:

Now, if I want to use the Student object in the Hello class, I will use the Setter method as follows:

Then, declare to inject the Student object into the Hello class in the declaration of the JobDetailFactoryBean class in the Spring container as follows:

Next is Trigger.

Spring also supports us in two ways to create a Trigger like Quartz Scheduler standalone, including Simple Trigger and Cron Trigger.

With Simple Trigger, we will use the SimpleTriggerFactoryBean class to create a trigger.

With the Cron Trigger, we will use the CronTriggerFactoryBean class:

If you notice, you will see the difference when declaring a Trigger in Spring is that Spring allows us to configure the Trigger to run for a Job. So, when configuring the Scheduler, we will not need to configure a Job for the Scheduler anymore.

Finally, the configuration for the Scheduler.

We will configure the Scheduler in Spring using the SchedulerFactoryBean class as follows:

OK, here we have configured Job, Trigger, and Scheduler of Quartz Scheduler with the Spring framework.

The content of the Spring configuration file will now be as follows:

I will create a new main class to run this application:

Result:

Using Quartz Scheduler with Spring framework

Add Comment