Easy Way to Configure Quartz Scheduler with Spring Boot – Example With Source Code

Quartz Scheduler is a 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 into a job that you can schedule and run as per your need. The library supports 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 a standalone application.

Configure Quartz Scheduler with Spring Boot:

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 a 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

Update – Adding DI to Spring Boot Quartz Scheduler Job Class

Few of our fellow developers were facing issues when tried to use @Inject in the Job Class. I could not pin point the root cause for that, but I found a solution. In earlier examples, I have used @Qualifier to inject JobDetail object and when we try to use Dependency Injection (Inject, Autowired) in the job class it failed. So instead we used @Qualifier to inject JobDetailFactoryBean instance. From this factory bean we then pulled JobDetails for further processing. Code Ref-

You can download and run the code from GitHub.

95 Comments
  1. Ankkt Tyagi
    March 8, 2021 | Reply
  2. Ankikt Tyagi
    March 4, 2021 | Reply
  3. Ramesh Komalli
    June 13, 2020 | Reply
  4. Dheeraj
    April 24, 2020 | Reply
  5. Apoorv Saxena
    February 9, 2020 | Reply
  6. priya
    December 11, 2019 | Reply
  7. Ajil
    August 21, 2019 | Reply
  8. Shrutika
    December 19, 2018 | Reply
  9. Amit Jain
    November 16, 2018 | Reply
    • Pavan
      November 16, 2018 | Reply
  10. yogi
    November 14, 2018 | Reply
    • Pavan
      November 16, 2018 | Reply
  11. Vishnu
    October 4, 2018 | Reply
    • Pavan
      October 4, 2018 | Reply
  12. Rehita
    September 14, 2018 | Reply
  13. August 30, 2018 | Reply
    • Shubham Jadhav
      January 6, 2020 | Reply
  14. Jamsheerali
    August 29, 2018 | Reply
  15. Kedar
    August 17, 2018 | Reply
  16. Chetan Mandlik
    July 16, 2018 | Reply
    • Pavan
      July 16, 2018 | Reply
      • Chetan Mandlik
        July 17, 2018 | Reply
        • Chetan Mandlik
          July 17, 2018 | Reply
          • Pavan
            July 18, 2018 |
      • Jamsheerali
        August 29, 2018 | Reply
  17. Rohith
    May 8, 2018 | Reply
    • Pavan
      May 9, 2018 | Reply
      • Rohith
        May 9, 2018 | Reply
        • Rohith
          May 9, 2018 | Reply
  18. Jay
    April 6, 2018 | Reply
    • Surya
      July 28, 2018 | Reply
  19. Kumar
    February 28, 2018 | Reply
    • Pavan
      March 1, 2018 | Reply
      • Kumar
        March 1, 2018 | Reply
  20. Archit Kumar
    February 26, 2018 | Reply
    • Pavan
      February 26, 2018 | Reply
      • Archit Kumar
        February 26, 2018 | Reply
        • Pavan
          February 26, 2018 | Reply
  21. Anurag
    February 14, 2018 | Reply
  22. Prasenjit
    January 27, 2018 | Reply
    • Pavan
      January 29, 2018 | Reply
  23. Jesus
    December 27, 2017 | Reply
    • Pavan
      December 27, 2017 | Reply
  24. elkk
    October 3, 2017 | Reply
  25. Madhuri
    September 8, 2017 | Reply
    • Pavan
      September 19, 2017 | Reply
  26. Alex
    August 29, 2017 | Reply
    • Pavan
      August 30, 2017 | Reply
  27. anil
    August 11, 2017 | Reply
    • Pavan
      August 11, 2017 | Reply
  28. 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 |
  29. Kay
    July 11, 2017 | Reply
  30. Kay
    June 30, 2017 | Reply
    • Pavan
      June 30, 2017 | Reply
      • Kay
        June 30, 2017 | Reply
        • Kay
          July 11, 2017 | Reply
          • Pavan
            July 11, 2017 |
  31. Kay
    June 30, 2017 | Reply
  32. Kay
    June 13, 2017 | Reply
    • Pavan
      June 16, 2017 | Reply
      • Kay
        June 29, 2017 | Reply
        • Kay
          June 29, 2017 | Reply
  33. 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 |
  34. Maxim
    May 11, 2017 | Reply
    • Pavan
      May 22, 2017 | Reply
    • huangmaoyi
      August 24, 2018 | Reply
  35. BharathyKannan
    April 28, 2017 | Reply
    • Pavan
      April 29, 2017 | Reply
  36. Bharat
    March 25, 2017 | Reply
    • Pavan
      March 25, 2017 | Reply
      • Bharat
        April 11, 2017 | Reply
        • Bharat
          April 12, 2017 | Reply
  37. Saurav
    March 2, 2017 | Reply
    • Pavan
      March 3, 2017 | Reply
  38. Jeffrey
    February 27, 2017 | Reply
  39. Deepthi
    February 2, 2017 | Reply
  40. akkil
    January 12, 2017 | Reply
    • Pavan
      January 12, 2017 | Reply
  41. akkil
    January 12, 2017 | Reply
  42. akkil
    January 11, 2017 | Reply
    • Pavan
      January 11, 2017 | Reply

Leave a Reply to Prasenjit Cancel reply

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