Spring boot with Quartz
What is Spring Boot?
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
(https://projects.spring.io/spring-boot/)
By using spring boot , your application will become quite simple to implement. You can focus more on your business without taking care of boring configurations.
What is quartz?
Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application
(http://www.quartz-scheduler.org/)
Create your first project with quartz
Create a new spring boot project in https://start.spring.io/, select the libraries you needed. Quartz can be supported since spring boot 2.0.0-M2. Let’s create a gradle project with spring boot 2.0.0-M3 in our cases.
After import your project in Intellij idea, you will find following dependencies with quartz
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-quartz')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Quartz provide a scheduler factory for you to get a scheduler, you can simply autowired SchedulerFactoryBean and invoke getScheduler() to grab a scheduler instance.
(http://www.quartz-scheduler.org/documentation/quartz-2.2.x/quick-start.html)
If you want to something more, such as have your own job. You need to implement the execute method of Job in quartz.
public class CustomizeJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("My customize job!");
}
}
Then you need to binding your job to JobDetail as defined in javadoc of Quartz.
Conveys the detail properties of a given Job
instance. JobDetails are to be created/defined with JobBuilder
.
JobDetail jobDetail = JobBuilder.newJob(CustomizeJob.class).withIdentity("testJob","group1").build();
When you try to set the trigger point of your job, you need to create your own trigger
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(4)
.repeatForever())
.build();
Then schedule your customize job by invoke
scheduler.scheduleJob(jobDetail,trigger);
You will find the print message in your console.
Something more than quick start?
If you want to get more information about the job, trigger and scheduler, you can implement the listener interface provided by quartz
As you can find in the methods list, you can use schedulerListener to track the progress of your job and scheduler such as by invoking jobPaused, jobResumed. JobListener can be used when a JobDetail executes. TriggerListener can be used to track trigger actions and status.
There are a lot advanced method supported in http://www.quartz-scheduler.org/api/2.1.7/. You can have more details in the document. Quartz is a quite useful library for scheduling. It is supported in spring boot 2.x, you can easily use it in your sprint boot projects. Enjoy!