Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application – from the smallest stand-alone application to the largest e-commerce system.
Quartz 是一个功能丰富,开源的任务调度的库, 可以和任何Java应用整合 .
1. 快速开始public class StdSchedulerTest{
public static void main(String[] args){
try {
//1. StdSchedulerFactory工厂机制加载一个Scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 2. JobBuilder 定义一个JobDetail , 工作信息
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
// 3. TriggerBuilder定义一个trigger , 触发器 , 告诉你这个Job何时触发
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInMilliseconds(1000)
.repeatForever())
.build();
// 4.告诉Scheduler , 我这个工作需要这个trigger
scheduler.scheduleJob(job, trigger);
//5. 启动
scheduler.start();
// 这里我们先阻塞着..
System.in.read();
// 6. 关闭
scheduler.shutdown();
} catch (SchedulerException | IOException se) {
se.printStackTrace();
}
}
}
其中 com.example.springquartz.HelloJob 需要实现org.quartz.Job 此接口public class HelloJob implements Job{