import static org.quartz.CronScheduleBuilder.cronSchedule; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import java.util.Date; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; public class QuartzManager { private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME"; private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME"; public static void addJob(String jobName, Class cls, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); // 任务 JobDetail job = newJob(cls).withIdentity(jobName, JOB_GROUP_NAME) .build(); // 触发器 CronTrigger trigger = newTrigger() .withIdentity(TRIGGER_GROUP_NAME, JOB_GROUP_NAME) .withSchedule(cronSchedule(time)).build(); sched.scheduleJob(job, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } } public static void addJob(String jobName, Class cls, int time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); // 任务 JobDetail job = newJob(cls).withIdentity(jobName, JOB_GROUP_NAME) .build(); // 触发器 SimpleTrigger trigger = (SimpleTrigger) newTrigger() .withIdentity(TRIGGER_GROUP_NAME, JOB_GROUP_NAME) .startAt(new Date()) .withSchedule( simpleSchedule().withIntervalInSeconds(time) .repeatForever()).build(); sched.scheduleJob(job, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { throw new RuntimeException(e); } } private static void startJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } private static void shutdownJobs() { try { Scheduler sched = gSchedulerFactory.getScheduler(); if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } public static void removeJob(String jobName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); // 停止触发器 sched.pauseTrigger(TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME)); // 移除触发器 sched.unscheduleJob(TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME)); // 删除任务 sched.deleteJob(JobKey.jobKey(jobName, JOB_GROUP_NAME)); } catch (Exception e) { throw new RuntimeException(e); } } public static void modifyJobTime(String jobName, String time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey .triggerKey(jobName, TRIGGER_GROUP_NAME)); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, JOB_GROUP_NAME)); Class objJobClass = jobDetail.getJobClass(); removeJob(jobName); addJob(jobName, objJobClass, time); } } catch (Exception e) { throw new RuntimeException(e); } } public static void modifyJobTime(String jobName, int time) { try { Scheduler sched = gSchedulerFactory.getScheduler(); SimpleTrigger trigger = (SimpleTrigger) sched.getTrigger(TriggerKey .triggerKey(jobName, TRIGGER_GROUP_NAME)); if (trigger == null) { return; } JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, JOB_GROUP_NAME)); Class objJobClass = jobDetail.getJobClass(); removeJob(jobName); addJob(jobName, objJobClass, time); } catch (Exception e) { throw new RuntimeException(e); } } public static boolean isExist(String jobName) { try { Scheduler sched = gSchedulerFactory.getScheduler(); SimpleTrigger trigger = (SimpleTrigger) sched.getTrigger(TriggerKey .triggerKey(jobName, TRIGGER_GROUP_NAME)); if (trigger == null) { return true; } else { return false; } } catch (Exception e) { throw new RuntimeException(e); } } }
import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class QuartzJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date())); } }
public class QuartzTest { public static void main(String[] args) { try { String job_name = "动态任务调度"; System.out.println("【系统启动】开始(每1秒输出一次)..."); QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?"); Thread.sleep(5000); System.out.println("【修改时间】开始(每2秒输出一次)..."); QuartzManager.modifyJobTime(job_name, "0/2 * * * * ?"); Thread.sleep(6000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(job_name); System.out.println("【移除定时】成功"); System.out.println("【再次添加定时任务】开始(每10秒输出一次)..."); QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?"); Thread.sleep(60000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(job_name); System.out.println("【移除定时】成功"); } catch (Exception e) { e.printStackTrace(); } } }