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.

48 Comments
  1. elkk
    October 3, 2017 | Reply
  2. Madhuri
    September 8, 2017 | Reply
    • Pavan
      September 19, 2017 | Reply
  3. Alex
    August 29, 2017 | Reply
    • Pavan
      August 30, 2017 | Reply
  4. anil
    August 11, 2017 | Reply
    • Pavan
      August 11, 2017 | Reply
  5. Kay
    July 13, 2017 | Reply
    • Pavan
      July 14, 2017 | Reply
      • Kay
        July 17, 2017 | Reply
        • Pavan
          July 17, 2017 | Reply
          • Kay
            August 23, 2017 |
          • Pavan
            August 25, 2017 |
  6. Kay
    July 11, 2017 | Reply
  7. Kay
    June 30, 2017 | Reply
    • Pavan
      June 30, 2017 | Reply
      • Kay
        June 30, 2017 | Reply
        • Kay
          July 11, 2017 | Reply
          • Pavan
            July 11, 2017 |
  8. Kay
    June 30, 2017 | Reply
  9. Kay
    June 13, 2017 | Reply
    • Pavan
      June 16, 2017 | Reply
      • Kay
        June 29, 2017 | Reply
        • Kay
          June 29, 2017 | Reply
  10. Karshit Jaiswal
    May 26, 2017 | Reply
    • Pavan
      May 29, 2017 | Reply
      • June 6, 2017 | Reply
        • Pavan
          June 6, 2017 | Reply
          • Karshit Jaiswal
            June 7, 2017 |
  11. Maxim
    May 11, 2017 | Reply
    • Pavan
      May 22, 2017 | Reply
  12. BharathyKannan
    April 28, 2017 | Reply
    • Pavan
      April 29, 2017 | Reply
  13. Bharat
    March 25, 2017 | Reply
    • Pavan
      March 25, 2017 | Reply
      • Bharat
        April 11, 2017 | Reply
        • Bharat
          April 12, 2017 | Reply
  14. Saurav
    March 2, 2017 | Reply
    • Pavan
      March 3, 2017 | Reply
  15. Jeffrey
    February 27, 2017 | Reply
    • Pavan
      March 1, 2017 | Reply
  16. Deepthi
    February 2, 2017 | Reply
  17. akkil
    January 12, 2017 | Reply
    • Pavan
      January 12, 2017 | Reply
  18. akkil
    January 12, 2017 | Reply
  19. akkil
    January 11, 2017 | Reply
    • Pavan
      January 11, 2017 | Reply

Leave a Reply to Pavan Cancel reply

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