簡介
完全由java編寫的開源作業(yè)調(diào)度框架
Quartz有兩大觸發(fā)器:SimpleTrigger和CronTrigger
使用CronTrigger要掌握Cron表達式
"30 * * * * ?" #每半分鐘觸發(fā)任務
"30 10 * * * ?" #每小時的10分30秒觸發(fā)任務
"30 10 1 * * ?" #每天1點10分30秒觸發(fā)任務
"30 10 1 20 * ?" #每月20號1點10分30秒觸發(fā)任務
.......
Maven
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
quartz.properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
HelloJob.java
public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("HelloJob...."+new Date());
}
}
QuartzTest
public static void main(String[] args) throws InterruptedException {
Scheduler scheduler;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
System.out.println("quartz start....");
scheduler.start();
/**********************doWork() start**************/
//每五秒執(zhí)行一次
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow().withSchedule(simpleScheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
TimeUnit.MINUTES.sleep(1);
/**********************doWork() end**************/
System.out.println("quartz shutdown....");
scheduler.shutdown();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
參考
官網(wǎng)quick-start
官網(wǎng)cookbook
【Quartz】Quartz的搭建拳氢、應用(單獨使用Quartz)
Quartz-v1.8.6
Quartz cron表達式格式