Quartz Scheduler with Spring Boot

Quartz Scheduler is widely accepted and used open source job scheduling library. This library can be integrated with all types of Java applications. Using this you can convert your simple java components in to a job that you can schedule and run as per your need. The library support various storage strategies like RAM, RDBMS etc. On top of this Quartz supports JTA transactions and clustering which comes in very handy when you want to deploy your application to production servers. We have earlier published an article for using Quartz in standalone application. Here in this article we will see how we can configure Spring Boot and Quartz. We will create a Spring Boot Quartz Scheduler application with MySQL as data store for our Quartz jobs and triggers.

Software used in this example

  • Spring Boot 1.4.1.RELEASE
  • Java 8
  • Quartz 2.2.1
  • MySQL
  • Maven
  • Eclipse

Demo Project Structure

The project will look like below in your Eclipse IDEQuartz Project Structure
Along with Spring annotations I am using xml based configuration to setup data source and entity manager for this example. To configure Quartz for your Spring Boot application you need to add below dependency to your pom.xml

As we are using MySQL as our database, we need to download quartz distributable and use the table structures that they have defined for MySQL. Once you have downloaded and executed the sql in your environment, the table will look like as below

Quartz Table Structure
This is our Application Start Point. You can see that I am using Spring Boot auto configuration capabilities here and at the same time I am importing my xml configuration as well.

We are defining a extra configuration class using annotation @Configuration that will do all required setup for Quartz.

You can see that above I am creating different factory beans that will be used to create jobs that can be run by either simple trigger or cron trigger. Also we are specifying the quartz properties file which will have different parameters to that are specific to quartz.

Also please note that we are configuring the Quartz based on a property in our application.properties file. The property you can find on head of the class. If this is set to true then only we will configure Quartz, else we will skip this configuration.

Now we are all set to define our first Job. The example below shows you a Job configured with Simple Trigger

Please make sure that the bean  names for Job and Trigger we define here are unique and referenced correctly. You can define as many jobs as you want in similar fashion and they will be scheduled automatically. No other configuration is needed. Thanks to Spring IOC, below code will look for all the Triggers that are defined as beans and will autowire them in a list which can be supplied to scheduler. Please check above ConfigureQuartz.java for code.

Lastly you can see below the output/console for the jobs that we have scheduled in our example.Quartz Output

You can download and run the code from GitHub.

17 Comments
  1. BharathyKannan
    April 28, 2017 | Reply
    • Pavan
      April 29, 2017 | Reply
  2. Bharat
    March 25, 2017 | Reply
    • Pavan
      March 25, 2017 | Reply
      • Bharat
        April 11, 2017 | Reply
        • Bharat
          April 12, 2017 | Reply
  3. Saurav
    March 2, 2017 | Reply
    • Pavan
      March 3, 2017 | Reply
  4. Jeffrey
    February 27, 2017 | Reply
    • Pavan
      March 1, 2017 | Reply
  5. Deepthi
    February 2, 2017 | Reply
  6. akkil
    January 12, 2017 | Reply
    • Pavan
      January 12, 2017 | Reply
  7. akkil
    January 12, 2017 | Reply
  8. akkil
    January 11, 2017 | Reply
    • Pavan
      January 11, 2017 | Reply

Add a Comment

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