Quartz, 是一個企業級排程工作的框架,幫助Java應用程式到排程工作/任務在指定的日期和時間執行。
本教學教作為一個入門介紹如何開發使用排程工作(在寫本教學時使用的最新Quartz 2.2.1 )
可以從官方網站或Maven中央儲存庫下載Quartz庫檔案;
File : quartz.properties
org.quartz.scheduler.instanceName = MyScheduler org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class =org.quartz.simpl.RAMJobStore
Quartz作業定義要執行什麼?
File : HelloJob
package com.tw511.common; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello Quartz!"); } }
定義Quartz觸發器,當Quartz執行在上面的Quartz作業。
像老版本的Quartz,仍然有兩種型別的觸發器在Quartz2,但API有些變化:
SimpleTrigger – 每5秒執行。
Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5).repeatForever()) .build();
CronTrigger – 每5秒執行。
Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build();
排程類連結“工作”和“觸發器”到一起,並執行它。
Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger);
Quartz2 兩個 SimpleTrigger 和 CronTrigger 完整的例子。
SimpleTrigger的例子 - 每間隔5秒執行。
package com.yiibai.quartz; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class SimpleTriggerExample { public static void main(String[] args) throws Exception { // Quartz 1.6.3 // JobDetail job = new JobDetail(); // job.setName("dummyJobName"); // job.setJobClass(HelloJob.class); JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("dummyJobName", "group1").build(); //Quartz 1.6.3 // SimpleTrigger trigger = new SimpleTrigger(); // trigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); // trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); // trigger.setRepeatInterval(30000); // Trigger the job to run on the next round minute Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5).repeatForever()) .build(); // schedule it Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } }
CronTrigger例子 - 同樣,在每5秒執行作業。
package com.yiibai.quartz; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class CronTriggerExample { public static void main( String[] args ) throws Exception { //Quartz 1.6.3 //JobDetail job = new JobDetail(); //job.setName("dummyJobName"); //job.setJobClass(HelloJob.class); JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("dummyJobName", "group1").build(); //Quartz 1.6.3 //CronTrigger trigger = new CronTrigger(); //trigger.setName("dummyTriggerName"); //trigger.setCronExpression("0/5 * * * * ?"); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("dummyTriggerName", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); //schedule it Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } }