1 EDITORIAL
Recently the company's business need to use regular tasks, frame selection using a Quartz, here as a learning record.
2 Quartz key members
Quartz core by three large Interface components, namely the Job , the Trigger , Scheduler
2.1 Job
Perform particular tasks described, and its structure is similar to the Runnable, only a method of the type void Execute (the JobExecutionContext context) , it is also a subclass InterruptableJob , represents a task may terminate
2.1.1 InterruptableJob
Based on the Job only added a void type of method interrupt () , for the termination of the task, it calls occur in QuartzScheduler # shutdown (boolean), which requires that we need to write like a termination of the write execute the same Runnable method.
2.2 Trigger
When used to describe the execute method of performing a Job
2.3 Scheduler
Similar queues and thread pools, storage of various information Job and Trigger, administrative tasks to add, remove, replace, operation, etc.
Preliminary use of 3 Quartz
Scheduler: As a management task container, Scheduler task and should have submitted a similar method to close the pool thread pool: add tasks corresponding method are the AddJob , the scheduleJob , very unfortunately signatures of these methods is JobDetail ;
JobDetail: JobDetail implementation class has a JobDetailImpl, and represents the empty constructor must specify the name , class , Group . It refers to a class Job implementation of the interface; Group is optional, use the default default;
Trigger: Trigger a plurality implementation classes, here only involves basically SimpleTriggerImpl;
Corresponding to the above-described construction has three interfaces or class factory class.
3.1 a common timing task
1 /** 2 * @author pancc 3 * @version 1.0 4 */ 5 public class SimpleJobDemo { 6 public static void main(String[] args) throws SchedulerException, InterruptedException { 7 JobDetail detail = JobBuilder.newJob(SimpleJob.class) 8 .withIdentity("simple", "group0") 9 .build(); 10 11 Trigger trigger = TriggerBuilder.newTrigger() 12 .withIdentity("simple_trigger") 13 .startNow() 14 .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2)) 15 .build(); 16 17 Scheduler scheduler = new StdSchedulerFactory().getScheduler(); 18 19 scheduler.start(); 20 scheduler.scheduleJob(detail, trigger); 21 /* 22 * 10 秒钟后关闭 23 */ 24 Thread.sleep(10_000); 25 scheduler.shutdown(); 26 } 27 28 public static class SimpleJob implements Job { 29 @Override 30 public void execute(JobExecutionContext context) { 31 System.out.printf("now date: %s", new Date()); 32 } 33 } 34 35 }
Top of the code creates a task Print task and specify its start time running time of submission, and the period of 2 seconds, last forever. 10 seconds after closing the container.
4 Create a task in the pit
The necessary attributes: JobDetail must have a descriptive name, use the default group when not set.
Accessibility: In a general code, visit our habitual restricted, such as the top of SimpleJob we might define private (meaning its constructor is private ), which is not allowed in Quartz. Job's empty constructor must be pubilc , which is related to the follow-up characteristics of Quartz.