1, the directory structure
2, the main file
All jar package "into the jar package, the quartz-2.2.2 unpack lib 1
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 ("The time now is:" + sf.format (DATE)); // specific business logic System.out.println ( "the Quartz the Hello" ); } }
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)) { ScheduleBuilder CronScheduleBuilder =CronScheduleBuilder.cronSchedule (newCron); // by the new reconstructed cronExpression expression trigger trigger = trigger.getTriggerBuilder () withIdentity (triggerKey) .withSchedule (scheduleBuilder) .build ();. // according to the new trigger reset job execution sched.rescheduleJob (triggerKey, Trigger); } } the catch (Exception E) { the throw new new a 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", "named group1" ); JobKey JobKey = JobKey.jobKey ( "cronjob", "named group1" ); the try { Scheduler sched = SchedulerUtil.getScheduler (); sched.pauseTrigger (triggerKey); // stop trigger sched.unscheduleJob (triggerKey); // remove trigger sched.deleteJob (JobKey); // delete the task } the catch (Exception E) { the throw new new a RuntimeException (E); } } }