Scheduler在quartz中负责调度作业的,用来绑定trigger和jobdetail。
用法如下:
public class TestHelloJob {
public static void main(String[] args) throws SchedulerException, InterruptedException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("current time : " + sf.format(new Date()));;
/* 创建JobDetail实例,并与HelloJob绑定;Job的名字是myjob,隶属于job_group组 */
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myjob1", "job_group1").build();
CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger()
.withIdentity("mytriggrt1", "mygroup1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/3 * * * * ? "))
.build();
/* 创建SchedulerFactory实例,把jobDetail与trigger绑定在一起 */
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
Date date = scheduler.scheduleJob(jobDetail, trigger);
System.out.println("date : " + date);
Thread.sleep(5000);
scheduler.standby(); //5秒之后暂停作业执行
Thread.sleep(10000);
scheduler.start(); //10秒之后重启作业
Thread.sleep(20000);
scheduler.shutdown(); //20秒之后停止作业执行
// scheduler.shutdown(true); //20秒后,如果作业执行完毕,则停止作业;如果作业未执行完毕,等待作业执行完毕,停止作业
// scheduler.shutdown(false);//20秒后,即使作业未执行完毕,也停止作业;默认为false
}
}