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:
Quartz • Java Persistence API • Representational state transfer • Job scheduler • Java (programming language) • Software development • Spring Framework • Apache Maven • Coupling (computer programming) • Quartz • Scheduling (computing) • Quartz • Quartz • Event-driven programming • Command-line interface • Cron • Quartz (graphics layer) • Scheduling (computing) • Booting • Application software • NeXTSTEP • Daemon (computing) • Scheduling (computing) • Execution (computing) • Statistics • Data • Project management • Information • Office • Scientific method • Interest • Social class • Service (economics) • Service (economics) • Project management • Server (computing) • Method (computer programming) • Method (computer programming) • Method (computer programming) • Booting • Server (computing) • Quartz (graphics layer) • Scheduling (computing) • Booting • Software repository • Quartz (graphics layer) • Scheduling (computing) • Daemon (computing) • Method (computer programming) • Systems analysis • Data • Daemon (computing) • Method (computer programming) • Class (computer programming) • Installation (computer programs) • Source code • Repository (version control) • NeXT • Representational state transfer • Interface (computing) • Quartz (graphics layer) • Scheduling (computing) •
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:
Quartz • Java Persistence API • Representational state transfer • Job scheduler • Java (programming language) • Software development • Spring Framework • Apache Maven • Coupling (computer programming) • Quartz • Scheduling (computing) • Quartz • Quartz • Event-driven programming • Command-line interface • Cron • Quartz (graphics layer) • Scheduling (computing) • Booting • Application software • NeXTSTEP • Daemon (computing) • Scheduling (computing) • Execution (computing) • Statistics • Data • Project management • Information • Office • Scientific method • Interest • Social class • Service (economics) • Service (economics) • Project management • Server (computing) • Method (computer programming) • Method (computer programming) • Method (computer programming) • Booting • Server (computing) • Quartz (graphics layer) • Scheduling (computing) • Booting • Software repository • Quartz (graphics layer) • Scheduling (computing) • Daemon (computing) • Method (computer programming) • Systems analysis • Data • Daemon (computing) • Method (computer programming) • Class (computer programming) • Installation (computer programs) • Source code • Repository (version control) • NeXT • Representational state transfer • Interface (computing) • Quartz (graphics layer) • Scheduling (computing) •