Adding Quartz to Spring Boot - 4 minutes read


Adding Quartz to Spring Boot

In my "Specifications to the Rescue" article, I presented the ease and ability to leverage the JPA Specification in Spring Boot to provide filter options for your RESTful API.  I followed up with another article, called "Testing those Specifications," which covered how those very specifications can be tested.

Taking things another step further, I thought I would demonstrate just how easy it is to add a job scheduler into the same Spring Boot application.

The team at Spring has continued to make Java development easier, by continuing to introduce Spring Boot Starters and providing baseline functionality for a specified integration via a simple Maven dependency. 

In this article, I am going to focus on the Quartz Scheduler starter, which can be added to a Spring Boot project by adding the following dependency:

The implementation is quite simple and is explained here. And you can review a full list of current Spring Boot Starters here.

Leveraging work published by David Kiss, the first step is to add auto-wiring support for Quartz jobs created in the project:

Next, we can add the base configuration for Quartz:

We could externalize the properties used in the   method but I decided to keep things really simple for this example.

Next, static methods are added to provide a programmatic way to schedule jobs and triggers:

The   method is a simple, helpful method to create jobs. Two trigger options exist, one for CRON-based triggers and the other for simple triggers.

At this point the base Quartz scheduler is ready to run jobs against our Spring Boot application.  The next step is to create some sample services to have the scheduler execute.

The first service is to create some very simple membership stats.  If you recall, the sample data in the original project was to track information related to a fitness club.  Within the   class, a  method was created:

To track fitness club interest in classes, a simple  method was created in the :

With the services in place, jobs need to be created to launch the code in the appropriate service. For the   job, I created the  class:

For this project, we want to make sure all jobs are scheduled when the Spring Boot server starts. In order to make this happen, I created the  class, which includes four simple methods. Two methods will create new jobs and two methods will create the appropriate triggers.

With everything ready, the Spring Boot server can be started and the Quartz scheduler initialized:

In the example above, I leveraged an existing Spring Boot repository and added the Quartz scheduler without a lot of effort.  From there, I was able to create service methods, which would perform a simple analysis against the data.  Those service methods were then launched by job classes.  Finally, the jobs and triggers were scheduled to complete the installation.

If you would like to see the full source code, my repository can be found here.

In my next article, I will demonstrate how a RESTful API can be added to an interface with the Quartz scheduler.

Source: Dzone.com

Powered by NewsAPI.org

Keywords:

QuartzJava Persistence APIRepresentational state transferJob schedulerJava (programming language)Software developmentSpring FrameworkApache MavenCoupling (computer programming)QuartzScheduling (computing)QuartzQuartzEvent-driven programmingCommand-line interfaceCronQuartz (graphics layer)Scheduling (computing)BootingApplication softwareNeXTSTEPDaemon (computing)Scheduling (computing)Execution (computing)StatisticsDataProject managementInformationOfficeScientific methodInterestSocial classService (economics)Service (economics)Project managementServer (computing)Method (computer programming)Method (computer programming)Method (computer programming)BootingServer (computing)Quartz (graphics layer)Scheduling (computing)BootingSoftware repositoryQuartz (graphics layer)Scheduling (computing)Daemon (computing)Method (computer programming)Systems analysisDataDaemon (computing)Method (computer programming)Class (computer programming)Installation (computer programs)Source codeRepository (version control)NeXTRepresentational state transferInterface (computing)Quartz (graphics layer)Scheduling (computing)