Spring Book – Chapter 19 – Tasks and Scheduling

Integration with Third-party Scheduling Service

Following the philosophy of Spring Framework, it does allow integrating other third party software’s with ease. In this section we will see the integration of Spring application with a popular scheduling service, Quartz, in full detail.


Quartz is a full-featured, open source job scheduling service that can be integrated with, or used alongside virtually any Java EE or Java SE application – from the smallest stand-alone application to the largest E-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that are programmed to fulfill the requirements of your application. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

–          Quartz Documentation

Integration with Spring

Integrating Quartz to Spring is a piece of cake as both Quartz and Spring has appropriate extensions to plug onto. To integrate your Spring application with Quartz, you will have to do the following steps as summarized below.

1. Project Dependency

Based on the build tool that you are using, put the Quartz dependency jars onto your Spring project.

2. Create Scheduler Class

This is the class which has the method which you would like to schedule using the quartz scheduler. Listing 20-20 shows the class and the single method it contains, which will be scheduled using Quartz scheduler.

Listing 20-20. Scheduled Class and its Method

3. Declare the Quartz Scheduler Job in the Spring configuration file

Declare the Quartz Scheduler Job in the Spring configuration file applicationContext.xml. The configuration of scheduler job can be done by two ways as given below:

a. Using MethodInvokingJobDetailFactoryBean

This is the simplest way to configure a scheduler in a Spring application. Using this way dependency of your class with the Spring API can be reduced as the configuration is in the configuration file. Listing 20-21 shows configuring this factory bean in the Spring configuration file as bean.

Listing 20-21. Configuring MethodInvokingJobDetailFactoryBean as spring bean

b. Using JobDetailBean

Using this method introduces dependency of your application to the Spring API’s for scheduling. First of all you will have to create a class extending org.springframework.scheduling.quartz.QuartzJobBean and implement the method “executeInternal” in which the method which needs scheduling to be called as shown in the Listing 20-22 below.

Listing 20-22. Creating JobDetailBean by extending QuartzJobBean

If you do this way of configuring the scheduler job, you will have to configure this scheduler job as shown in Listing 20-23 below.

Listing 20-23. Configuring JobDetailBean class

4. Configure trigger in Spring configuration file.

Configure Quartz trigger which will decide when to run the job which is configured already in previous steps. Two different types of triggers can be created and configured. They are:

a. Simple Trigger

Configuration of simple trigger is as shown in Listing 20-24 below.

Listing 20-24. Configuring simple trigger in Spring configuration

b. Cron Trigger

Configuration of simple trigger is as shown in Listing 20-25 below.

Listing 20-25. Configuring simple trigger in Spring configuration

5. Configure the scheduler factory.

Link the job and the trigger using the scheduler factory as shown in Listing 20-26 below.

Listing 20-26. Configure scheduler factory in Spring configuration file

6. Test class

Test class for seeing the scheduling of job using Quartz in action is as shown in Listing 20-27 below.

Listing 20-27. Test class

Very simple right, actually nothing needs to be done after bootstrapping the Spring configuration file because the job will be run according to the schedule that we have configured using trigger.

Page Visitors: 24516

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *