1、目录结构
2、主要文件
1》引入的jar包,quartz-2.2.2解压后lib下所有的jar包
2》quartz.properties
org.quartz.threadPool.threadCount: 2
org.quartz.jobStore.misfireThreshold: 4000
3》SchedulerUtil.java
package com.lfy.scheduler; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; public class SchedulerUtil { private static Scheduler scheduler; private static CronTrigger cronTrigger; private static JobDetail jobDetail; public static Scheduler getScheduler() { return scheduler; } public static void setScheduler(Scheduler scheduler) { SchedulerUtil.scheduler = scheduler; } public static CronTrigger getCronTrigger() { return cronTrigger; } public static void setCronTrigger(CronTrigger cronTrigger) { SchedulerUtil.cronTrigger = cronTrigger; } public static JobDetail getJobDetail() { return jobDetail; } public static void setJobDetail(JobDetail jobDetail) { SchedulerUtil.jobDetail = jobDetail; } }
4》HelloJob.java
package com.lfy.jobdetail; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("现在的时间是:"+ sf.format(date)); //具体的业务逻辑 System.out.println("Hello Quartz"); } }
5》CronTriggerScheduleApp.java
package com.lfy.controller; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.SchedulerException; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lfy.jobdetail.HelloJob; import com.lfy.scheduler.SchedulerUtil; @Controller @RequestMapping("/App") public class CronTriggerScheduleApp { @RequestMapping(value="/startQuartz") public String startQuartz() throws SchedulerException { SchedulerUtil.setScheduler(new StdSchedulerFactory().getScheduler()); SchedulerUtil.setJobDetail(JobBuilder.newJob(HelloJob.class) .withIdentity("cronJob","group1") .build()); CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule("/6 * * * * ? *"); cronScheduleBuilder.withMisfireHandlingInstructionDoNothing(); SchedulerUtil.setCronTrigger(TriggerBuilder.newTrigger() .withIdentity("cronTrigger") .withSchedule(cronScheduleBuilder) .build()); SchedulerUtil.getScheduler().start(); SchedulerUtil.getScheduler().scheduleJob(SchedulerUtil.getJobDetail(), SchedulerUtil.getCronTrigger()); return "index"; } }
6》PauseController.java
package com.lfy.controller; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lfy.scheduler.SchedulerUtil; @Controller @RequestMapping("/PauseController") public class PauseController { @RequestMapping(value="/pause") public void pauseCronTrigger() { JobKey jobkey=JobKey.jobKey("cronJob","group1"); Scheduler scheduler=SchedulerUtil.getScheduler(); try { scheduler.pauseJob(jobkey); } catch (SchedulerException e) { e.printStackTrace(); } } }
7》ResumeController.java
package com.lfy.controller; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lfy.scheduler.SchedulerUtil; @Controller @RequestMapping("/ResumeController") public class ResumeController { @RequestMapping(value="/resume") public void resumeCronTrigger() { JobKey jobkey=JobKey.jobKey("cronJob","group1"); Scheduler scheduler=SchedulerUtil.getScheduler(); try { scheduler.resumeJob(jobkey); } catch (SchedulerException e) { e.printStackTrace(); } } }
8》AlterTriggerTimeController.java
package com.lfy.controller; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Scheduler; import org.quartz.TriggerKey; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lfy.scheduler.SchedulerUtil; @Controller @RequestMapping("/RescheduleController") public class AlterTriggerTimeController { @RequestMapping(value="/reschedule") public void rescheduleCronTrigger() { String newCron="/3 * * * * ? *"; TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1"); try { Scheduler sched = SchedulerUtil.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(newCron)) { CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(newCron); // 按新的cronExpression表达式重新构建trigger trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); // 按新的trigger重新设置job执行 sched.rescheduleJob(triggerKey, trigger); } } catch (Exception e) { throw new RuntimeException(e); } } }
9》DeleteController.java
package com.lfy.controller; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.TriggerKey; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lfy.scheduler.SchedulerUtil; @Controller @RequestMapping("/DeleteController") public class DeleteController { @RequestMapping(value="/delete") public void deleteCronTrigger() { TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1"); JobKey jobKey = JobKey.jobKey("cronJob","group1"); try { Scheduler sched = SchedulerUtil.getScheduler(); sched.pauseTrigger(triggerKey);// 停止触发器 sched.unscheduleJob(triggerKey);// 移除触发器 sched.deleteJob(jobKey);// 删除任务 } catch (Exception e) { throw new RuntimeException(e); } } }