package com.example.singleQuartz; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.Date; import java.util.concurrent.TimeUnit; /** * Created by XuHui on 2016/12/22. */ public class SingleQuartzThreadPoolServer { public static void main(String[] args) throws SchedulerException { /* 获取Scheduler实例 */ SchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); /* 启动调度器 */ scheduler.start(); /******************************************************* Job-1 ****************************************************************/ /* 任务:withIdentity(String name, String group),其中groupName可以自己定义,也可以用Scheduler提供的DEFAULT_GROUP。 这里要求同组里面的jobName不能相同*/ JobDetail jobDetail = JobBuilder.newJob(SingleQuartzJob.class) .withIdentity("job1", Scheduler.DEFAULT_GROUP) .build(); /*触发器:SimpleTrigger和CronTrigger。实现的功能是:接下来30s后执行job,以后每个10s执行一次,重复10次,一共执行11次。 nextGivenSecondDate(null, 30)距离现在时间30s之后执行job,此处null可写作new Date(),可自行在api查看源码实现 */ Date startTime = DateBuilder.nextGivenSecondDate(null, 30); SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", Scheduler.DEFAULT_GROUP) .startAt(startTime) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(2)) .forJob(jobDetail) .build(); /* 交由调度器调度Job */ scheduler.scheduleJob(jobDetail, simpleTrigger); /******************************************************* Job-2 ****************************************************************/ jobDetail = JobBuilder.newJob(SingleQuartzJob.class) .withIdentity("job2", Scheduler.DEFAULT_GROUP) .build(); simpleTrigger = TriggerBuilder.newTrigger() .withIdentity("trigger2", Scheduler.DEFAULT_GROUP) .startAt(startTime) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(2)) .forJob(jobDetail) .build(); scheduler.scheduleJob(jobDetail, simpleTrigger); /******************************************************* Job-3 ****************************************************************/ jobDetail = JobBuilder.newJob(SingleQuartzJob.class) .withIdentity("job3", Scheduler.DEFAULT_GROUP) .build(); simpleTrigger = TriggerBuilder.newTrigger() .withIdentity("trigger3", Scheduler.DEFAULT_GROUP) .startAt(startTime) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(2)) .forJob(jobDetail) .build(); scheduler.scheduleJob(jobDetail, simpleTrigger); /* 2分钟任务调度之后关闭调度器 */ try{ TimeUnit.MINUTES.sleep(2); } catch (Exception e) { e.printStackTrace(); } finally { scheduler.shutdown(); } } }
执行结果如下:
[main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:30 CST 2016 by DEFAULT.job1 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:30 CST 2016 by DEFAULT.job2 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:30 CST 2016 by DEFAULT.job3 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:40 CST 2016 by DEFAULT.job3 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:40 CST 2016 by DEFAULT.job1 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:40 CST 2016 by DEFAULT.job2 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:50 CST 2016 by DEFAULT.job1 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:50 CST 2016 by DEFAULT.job3 Hello, Quartz! - executing its JOB at Thu Dec 22 19:52:50 CST 2016 by DEFAULT.job2 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
(二)任务调度持久到数据库
1.建立数据库和建表