转发的代码,原理就不提了
下面直接上代码:
QuartzManager.java 动态添加、修改和删除定时任务管理类
1 import org.quartz.CronScheduleBuilder; 2 import org.quartz.CronTrigger; 3 import org.quartz.Job; 4 import org.quartz.JobBuilder; 5 import org.quartz.JobDetail; 6 import org.quartz.JobKey; 7 import org.quartz.Scheduler; 8 import org.quartz.SchedulerFactory; 9 import org.quartz.Trigger; 10 import org.quartz.TriggerBuilder; 11 import org.quartz.TriggerKey; 12 import org.quartz.impl.StdSchedulerFactory; 13 14 public class QuartzManager { 15 16 private static SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 17 18 /** 19 * @Description: 添加一个定时任务 20 * 21 * @param jobName 任务名 22 * @param jobGroupName 任务组名 23 * @param triggerName 触发器名 24 * @param triggerGroupName 触发器组名 25 * @param jobClass 任务 26 * @param cron 时间设置,参考quartz说明文档 27 */ 28 @SuppressWarnings({ "unchecked", "rawtypes" }) 29 public static void addJob(String jobName, String jobGroupName, 30 String triggerName, String triggerGroupName, Class jobClass, String cron) { 31 try { 32 Scheduler sched = schedulerFactory.getScheduler(); 33 // 任务名,任务组,任务执行类 34 JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build(); 35 36 // 触发器 37 TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); 38 // 触发器名,触发器组 39 triggerBuilder.withIdentity(triggerName, triggerGroupName); 40 triggerBuilder.startNow(); 41 // 触发器时间设定 42 triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); 43 // 创建Trigger对象 44 CronTrigger trigger = (CronTrigger) triggerBuilder.build(); 45 46 // 调度容器设置JobDetail和Trigger 47 sched.scheduleJob(jobDetail, trigger); 48 49 // 启动 50 if (!sched.isShutdown()) { 51 sched.start(); 52 } 53 } catch (Exception e) { 54 throw new RuntimeException(e); 55 } 56 } 57 58 /** 59 * @Description: 修改一个任务的触发时间 60 * 61 * @param jobName 62 * @param jobGroupName 63 * @param triggerName 触发器名 64 * @param triggerGroupName 触发器组名 65 * @param cron 时间设置,参考quartz说明文档 66 */ 67 public static void modifyJobTime(String jobName, 68 String jobGroupName, String triggerName, String triggerGroupName, String cron) { 69 try { 70 Scheduler sched = schedulerFactory.getScheduler(); 71 TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); 72 CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); 73 if (trigger == null) { 74 return; 75 } 76 77 String oldTime = trigger.getCronExpression(); 78 if (!oldTime.equalsIgnoreCase(cron)) { 79 /** 方式一 :调用 rescheduleJob 开始 */ 80 // 触发器 81 TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); 82 // 触发器名,触发器组 83 triggerBuilder.withIdentity(triggerName, triggerGroupName); 84 triggerBuilder.startNow(); 85 // 触发器时间设定 86 triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron)); 87 // 创建Trigger对象 88 trigger = (CronTrigger) triggerBuilder.build(); 89 // 方式一 :修改一个任务的触发时间 90 sched.rescheduleJob(triggerKey, trigger); 91 /** 方式一 :调用 rescheduleJob 结束 */ 92 93 /** 方式二:先删除,然后在创建一个新的Job */ 94 //JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName)); 95 //Class<? extends Job> jobClass = jobDetail.getJobClass(); 96 //removeJob(jobName, jobGroupName, triggerName, triggerGroupName); 97 //addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron); 98 /** 方式二 :先删除,然后在创建一个新的Job */ 99 } 100 } catch (Exception e) { 101 throw new RuntimeException(e); 102 } 103 } 104 105 /** 106 * @Description: 移除一个任务 107 * 108 * @param jobName 109 * @param jobGroupName 110 * @param triggerName 111 * @param triggerGroupName 112 */ 113 public static void removeJob(String jobName, String jobGroupName, 114 String triggerName, String triggerGroupName) { 115 try { 116 Scheduler sched = schedulerFactory.getScheduler(); 117 118 TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); 119 120 sched.pauseTrigger(triggerKey);// 停止触发器 121 sched.unscheduleJob(triggerKey);// 移除触发器 122 sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务 123 } catch (Exception e) { 124 throw new RuntimeException(e); 125 } 126 } 127 128 /** 129 * @Description:启动所有定时任务 130 */ 131 public static void startJobs() { 132 try { 133 Scheduler sched = schedulerFactory.getScheduler(); 134 sched.start(); 135 } catch (Exception e) { 136 throw new RuntimeException(e); 137 } 138 } 139 140 /** 141 * @Description:关闭所有定时任务 142 */ 143 public static void shutdownJobs() { 144 try { 145 Scheduler sched = schedulerFactory.getScheduler(); 146 if (!sched.isShutdown()) { 147 sched.shutdown(); 148 } 149 } catch (Exception e) { 150 throw new RuntimeException(e); 151 } 152 } 153 }