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 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 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 extend 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 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 Spring container and targetMethod which will have the value as the method name in Hello class, will execute the task.

For the second way we need to create a new class extend abstract class QuartzJobBean (if you see the code of QuartzJobBean, you will see this class implemented 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 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 class Hello, 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 Student object in Hello class, I will use the Setter method as follows:

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




Next is Trigger.

Spring also supports us two ways to create 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 Trigger in Spring is that Spring allows us to configure the Trigger to run for a Job.So, when configuring Scheduler, we will not need to configure Job for Scheduler anymore.



Finally, the configuration for Scheduler.

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

OK, here we have configured Job, Trigger, and Scheduler of Quartz Scheduler with 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