1. Get to know Quartz
1.1 overview
-
Quartz is an open source task scheduling framework written entirely in Java. It is simple to say that developers can schedule tasks according to time intervals through quartz, for example:
- Command the program to perform a task every hour
- Command the program to perform one task every month
- Specify the end of a month to command the program to perform a task
- ......
- Command the program to perform a task every hour
- Quartz download address: http://www.quartz-scheduler.org/downloads/
1.2,Quartz API
1.2.1 scheduler -- the main API for interaction with the scheduler;
The life cycle of the Scheduler starts when the SchedulerFactory creates it and ends when the Scheduler calls the showdown() method. The Scheduler will actually Trigger trigger (execute job) only after scheduling the start() method. After the Scheduler is created, you can add, delete, list jobs and triggers, and perform other scheduling related operations.
1.2.2 Job -- interface to be implemented for tasks executed by scheduler
1.2.3 Job detail -- define the instance of Job
1.2.4 Trigger - Trigger the execution of Job
1.2.5 JobBuilder -- define the interface for creating JobDetail instance
1.2.6 Trigger builder -- define the interface for creating Trigger instance
2. Simple application of Hello Quartz
2.1. First, implement a simple small demo "Hello quartz!". Specific requirements: output a sentence "Hello quartz!" through the console every 5 seconds;
2.2, code
1 package com.xxw.test; 2 3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 7 public class MyJob implements Job{ 8 9 @Override 10 public void execute(JobExecutionContext arg0) throws JobExecutionException { 11 System.out.println("hello quartz!"); 12 } 13 14 }
1 package com.xxw.test; 2 3 import static org.quartz.JobBuilder.newJob; 4 import static org.quartz.SimpleScheduleBuilder.simpleSchedule; 5 import static org.quartz.TriggerBuilder.newTrigger; 6 7 import org.quartz.JobDetail; 8 import org.quartz.Scheduler; 9 import org.quartz.Trigger; 10 import org.quartz.impl.StdSchedulerFactory; 11 12 public class HellowQuartz { 13 14 public static void main(String[] args) { 15 try { 16 // Definition JobDetail 17 JobDetail job = newJob(MyJob.class) // Definition Job Class is MyJob Class, which is the task logic class 18 .withIdentity("job1", "group1") // Definition name/group 19 .build(); 20 21 // Define a Trigger 22 Trigger trigger = newTrigger() 23 .withIdentity("trigger1", "group1") // Definition name/group 24 .startNow() // Once joined scheduler,Effective immediately 25 .withSchedule( 26 simpleSchedule() // Use simpleSchedule 27 .withIntervalInSeconds(5) // Every 5 seconds 28 .repeatForever()) // Always execute 29 .build(); 30 31 // Example Schedule Scheduler 32 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 33 // Add scheduling 34 scheduler.scheduleJob(job, trigger); 35 // start-up 36 scheduler.start(); 37 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } 41 } 42 }