动态添加、修改、删除定时任务
近日在做项目的时候,要在前台配置定时任务才发现可以动态的添加、修改和删除定时任务,现在用一个简单的例子做下记录。
首先,添加一个定时任务管理类QuartzManager:
package com.breeze.job; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; public class QuartzManager { private Scheduler scheduler; private static QuartzManager instance = null; private QuartzManager() { try { scheduler = new StdSchedulerFactory().getScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 获取定时器调度实例 * * @return 实例对象 */ public static QuartzManager getInstance() { if (null == instance) { synchronized (QuartzManager.class) { if (null == instance) { instance = new QuartzManager(); } } } return instance; } /** * 功能: 添加一个定时任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务的类类型 eg:TimedMassJob.class * @param cron * 时间设置 表达式,参考quartz说明文档 */ public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class<? extends Job> jobClass, String cron, Object... objects) { try { // 任务名,任务组,任务执行类 JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(jobName, jobGroupName).build(); // 触发器 if (objects != null) { for (int i = 0; i < objects.length; i++) { jobDetail.getJobDataMap().put("data" + (i + 1), objects[i]); } } TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder .newTrigger(); // 触发器名,触发器组 triggerBuilder.withIdentity(triggerName, triggerGroupName); triggerBuilder.startNow(); // 触发器时间设定 triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); // 创建Trigger对象 CronTrigger trigger = (CronTrigger) triggerBuilder.build(); // 调度容器设置JobDetail和Trigger scheduler.scheduleJob(jobDetail, trigger); } catch (Exception e) { throw new RuntimeException(e); } } /** * 功能:修改一个任务的触发时间 * * @param jobName * @param jobGroupName * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param cron * 时间设置,参考quartz说明文档 */ public void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName, String cron) { try { TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); CronTrigger trigger = (CronTrigger) scheduler .getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(cron)) { // 触发器 TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder .newTrigger(); // 触发器名,触发器组 triggerBuilder.withIdentity(triggerName, triggerGroupName); triggerBuilder.startNow(); // 触发器时间设定 triggerBuilder.withSchedule(CronScheduleBuilder .cronSchedule(cron)); // 创建Trigger对象 trigger = (CronTrigger) triggerBuilder.build(); // 方式一 :修改一个任务的触发时间 scheduler.rescheduleJob(triggerKey, trigger); } } catch (Exception e) { throw new RuntimeException(e); } } /** * 功能: 移除一个任务 * * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName */ public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); // 停止触发器 scheduler.pauseTrigger(triggerKey); // 移除触发器 scheduler.unscheduleJob(triggerKey); // 删除任务 scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName)); } catch (Exception e) { throw new RuntimeException(e); } } /** * * 功能:启动所有定时任务 */ public void startJobs() { try { if (scheduler.isStarted()) { System.out.println("定时器已启动。"); } else { scheduler.start(); } } catch (SchedulerException e1) { e1.printStackTrace(); } } /** * 功能:关闭所有定时任务 */ public void shutdownJobs() { try { if (!scheduler.isShutdown()) { scheduler.shutdown(); } } catch (SchedulerException e) { e.printStackTrace(); } } }
再添加两个人物类
package com.breeze.job; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class A implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { JobDataMap jobDataMap = arg0.getJobDetail().getJobDataMap(); String arg = (String) jobDataMap.get("data1"); String arg1 = (String) jobDataMap.get("data2"); System.out.println("Go job A..." + arg + "..." + arg1 + "..." + new Date()); } }
package com.breeze.job; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class B implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { JobDataMap jobDataMap = arg0.getJobDetail().getJobDataMap(); String arg = (String) jobDataMap.get("data1"); System.out.println("Go job B..." + arg + "..." + new Date()); } }
对定时任务做添加修改、删除、操作。
package foo; import java.util.Date; import com.breeze.job.A; import com.breeze.job.B; import com.breeze.job.QuartzManager; public class App { public static void main(String[] args) { // 添加任务A QuartzManager.getInstance().addJob("A", "001", "A", "001", A.class, "0/5 * * * * ?", "A", "C"); QuartzManager.getInstance().startJobs(); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date()); // 修改任务A的出发时间设定 QuartzManager.getInstance().modifyJobTime("A", "001", "A", "001", "0/4 * * * * ?"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date()); // 添加任务B QuartzManager.getInstance().addJob("B", "002", "B", "002", B.class, "0/6 * * * * ?", "B"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date()); // 删除任务A QuartzManager.getInstance().removeJob("A", "001", "A", "001"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } QuartzManager.getInstance().shutdownJobs(); } }