Quartz2第一個程序
Quartz, 是一個企業級調度工作的框架,幫助Java應用程序到調度工作/任務在指定的日期和時間運行。
本教程教作爲一個入門介紹如何開發使用調度工作(在寫本教程時使用的最新Quartz 2.2.1 )
1. 下載Quartz
可以從官方網站或Maven中央存儲庫下載Quartz庫文件;
File : quartz.properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class =org.quartz.simpl.RAMJobStore
2. Quartz 作業
Quartz作業定義要運行什麼?
File : HelloJob
package com.yiibai.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!");
}
}
3. Quartz觸發器
定義Quartz觸發器,當Quartz運行在上面的Quartz作業。
像老版本的Quartz,仍然有兩種類型的觸發器在Quartz2,但API有些變化:
- SimpleTrigger – 允許設置開始時間,結束時間,重複間隔。
- CronTrigger – 允許UNIX cron表達式來指定日期和時間來運行作業。
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();
注意
請閱讀 官方文檔 更多的Quartz2觸發器的例子。
4. Scheduler
調度類鏈接「工作」和「觸發器」到一起,並執行它。
Scheduler scheduler \= new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
5. 完整的例子
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);
}
}