QuartzJob.java
//持久化 @PersistJobDataAfterExecution //禁止并发执行(Quartz不要并发地执行同一个job定义(这里指一个job类的多个实例)) @DisallowConcurrentExecution @Slf4j public class QuartzJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { String taskName = context.getJobDetail().getJobDataMap().getString("name"); log.info("---> Quartz job {}, {} <----", new Date(), taskName); } }QuartzJob2.java
@PersistJobDataAfterExecution @DisallowConcurrentExecution @Slf4j public class QuartzJob2 extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { String taskName = context.getJobDetail().getJobDataMap().getString("name"); log.info("---> Quartz job 2 {}, {} <----", new Date(), taskName); } } 4.初始化触发器等信息,这里通过Listener初始化 @Slf4j public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired SchedulerConfig schedulerConfig; public static AtomicInteger count = new AtomicInteger(0); private static String TRIGGER_GROUP_NAME = "test_trriger"; private static String JOB_GROUP_NAME = "test_job"; @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 防止重复执行 if (event.getApplicationContext().getParent() == null && count.incrementAndGet() <= 1) { initMyJob(); } } public void initMyJob() { Scheduler scheduler = null; try { scheduler = schedulerConfig.scheduler(); TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", TRIGGER_GROUP_NAME); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (null == trigger) { Class clazz = QuartzJob.class; JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity("job1", JOB_GROUP_NAME).build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?"); trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", TRIGGER_GROUP_NAME) .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); log.info("Quartz 创建了job:...:{}", jobDetail.getKey()); } else { log.info("job已存在:{}", trigger.getKey()); } TriggerKey triggerKey2 = TriggerKey.triggerKey("trigger2", TRIGGER_GROUP_NAME); CronTrigger trigger2 = (CronTrigger) scheduler.getTrigger(triggerKey2); if (null == trigger2) { Class clazz = QuartzJob2.class; JobDetail jobDetail2 = JobBuilder.newJob(clazz).withIdentity("job2", JOB_GROUP_NAME).build(); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?"); trigger2 = TriggerBuilder.newTrigger().withIdentity("trigger2", TRIGGER_GROUP_NAME) .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail2, trigger2); log.info("Quartz 创建了job:...:{}", jobDetail2.getKey()); } else { log.info("job已存在:{}", trigger2.getKey()); } scheduler.start(); } catch (Exception e) { log.info(e.getMessage()); } } } 5.启动定时器启动两个Application,分别是示例中的DemoQuartzApplication和DemoQuartzApplication2,会发现,两个Job会分别在两个应用执行。
当手动停止一个应用的时候,另一个应用会自动接管所有任务并继续执行,如果任务太多,我们可以再开一台服务即可。实现了调度任务的高可用性和可扩展性
运行效果如图:
示例代码-github
spring-scheduling参考