springboot集成quartz定时任务课动态执行
1 <dependency> 2 <groupId>org.quartz-scheduler</groupId> 3 <artifactId>quartz</artifactId> 4 <version>2.2.1</version> 5 </dependency> 6 <dependency><!-- 该依赖必加,里面有sping对schedule的支持 --> 7 <groupId>org.springframework</groupId> 8 <artifactId>spring-context-support</artifactId> 9 </dependency> 10 <!--必须添加,要不然会出错,项目无法启动--> 11 <dependency> 12 <groupId>org.springframework</groupId> 13 <artifactId>spring-tx</artifactId> 14 <version>4.3.16.RELEASE</version> 15 </dependency>
1 ** 2 * 描述: 3 * task-任务调度处理 4 * 5 * @Author zuoxh 6 * @create 2018-12-19 20:38 7 */ 8 @Configuration 9 public class QuartzConfig { 10 11 @Resource 12 private Scheduler scheduler; 13 14 /** 15 * 开始执行所有任务 16 * 17 * @throws SchedulerException 18 */ 19 public void startJob() throws SchedulerException { 20 startJob1(scheduler); 21 startJob2(scheduler); 22 scheduler.start(); 23 } 24 25 /** 26 * 获取Job信息 27 * 28 * @param name 29 * @param group 30 * @return 31 * @throws SchedulerException 32 */ 33 public String getJobInfo(String name, String group) throws SchedulerException { 34 TriggerKey triggerKey = new TriggerKey(name, group); 35 CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey); 36 return String.format("time:%s,state:%s", cronTrigger.getCronExpression(), 37 scheduler.getTriggerState(triggerKey).name()); 38 } 39 40 /** 41 * 修改某个任务的执行时间 42 * 43 * @param name 44 * @param group 45 * @param time 46 * @return 47 * @throws SchedulerException 48 */ 49 public boolean modifyJob(String name, String group, String time) throws SchedulerException { 50 Date date = null; 51 TriggerKey triggerKey = new TriggerKey(name, group); 52 CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey); 53 String oldTime = cronTrigger.getCronExpression(); 54 if (!oldTime.equalsIgnoreCase(time)) { 55 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time); 56 CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group) 57 .withSchedule(cronScheduleBuilder).build(); 58 date = scheduler.rescheduleJob(triggerKey, trigger); 59 } 60 return date != null; 61 } 62 63 /** 64 * 暂停所有任务 65 * 66 * @throws SchedulerException 67 */ 68 public void pauseAllJob() throws SchedulerException { 69 scheduler.pauseAll(); 70 } 71 72 /** 73 * 暂停某个任务 74 * 75 * @param name 76 * @param group 77 * @throws SchedulerException 78 */ 79 public void pauseJob(String name, String group) throws SchedulerException { 80 JobKey jobKey = new JobKey(name, group); 81 JobDetail jobDetail = scheduler.getJobDetail(jobKey); 82 if (jobDetail == null) 83 return; 84 scheduler.pauseJob(jobKey); 85 } 86 87 /** 88 * 恢复所有任务 89 * 90 * @throws SchedulerException 91 */ 92 public void resumeAllJob() throws SchedulerException { 93 scheduler.resumeAll(); 94 } 95 96 /** 97 * 恢复某个任务 98 * 99 * @param name 100 * @param group 101 * @throws SchedulerException 102 */ 103 public void resumeJob(String name, String group) throws SchedulerException { 104 JobKey jobKey = new JobKey(name, group); 105 JobDetail jobDetail = scheduler.getJobDetail(jobKey); 106 if (jobDetail == null) 107 return; 108 scheduler.resumeJob(jobKey); 109 } 110 111 /** 112 * 删除某个任务 113 * 114 * @param name 115 * @param group 116 * @throws SchedulerException 117 */ 118 public void deleteJob(String name, String group) throws SchedulerException { 119 JobKey jobKey = new JobKey(name, group); 120 JobDetail jobDetail = scheduler.getJobDetail(jobKey); 121 if (jobDetail == null) 122 return; 123 scheduler.deleteJob(jobKey); 124 } 125 126 private void startJob1(Scheduler scheduler) throws SchedulerException { 127 // 通过JobBuilder构建JobDetail实例,JobDetail规定只能是实现Job接口的实例 128 // JobDetail 是具体Job实例 129 JobDetail jobDetail = JobBuilder.newJob(FirstTask.class).withIdentity("firstTask", "firstTask").build(); 130 // 基于表达式构建触发器 131 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?"); 132 // CronTrigger表达式触发器 继承于Trigger 133 // TriggerBuilder 用于构建触发器实例 134 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("firstTask", "firstTask") 135 .withSchedule(cronScheduleBuilder).build(); 136 scheduler.scheduleJob(jobDetail, cronTrigger); 137 } 138 139 private void startJob2(Scheduler scheduler) throws SchedulerException { 140 JobDetail jobDetail = JobBuilder.newJob(SecondTask.class).withIdentity("secondTask", "secondTask").build(); 141 CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?"); 142 CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("secondTask", "secondTask") 143 .withSchedule(cronScheduleBuilder).build(); 144 scheduler.scheduleJob(jobDetail, cronTrigger); 145 } 146 }
配置任务和配置quartz
1 /** 2 * 描述: 3 * task- 4 * 5 * @Author zuoxh 6 * @create 2018-12-19 20:22 7 */ 8 public class FirstTask implements Job { 9 @Override 10 public void execute(JobExecutionContext context) throws JobExecutionException { 11 // TODO 业务 12 System.out.println("结束1:"+System.currentTimeMillis()); 13 } 14 }
1 /** 2 * 描述: 3 * task- 4 * 5 * @Author zuoxh 6 * @create 2018-12-19 20:25 7 */ 8 public class SecondTask implements Job { 9 @Override 10 public void execute(JobExecutionContext context) throws JobExecutionException { 11 // TODO 业务 12 System.out.println("结束2:"+System.currentTimeMillis()); 13 } 14 }
配置多多个任务
1 /** 2 * 描述: 3 * task- 4 * 5 * @Author zuoxh 6 * @create 2018-12-19 20:47 7 */ 8 @Controller 9 public class QuartzApiController { 10 11 @Autowired 12 private QuartzConfig quartzConfig; 13 14 @RequestMapping("/start") 15 @ResponseBody 16 public String startQuartzJob() { 17 try { 18 quartzConfig.startJob(); 19 return "start"; 20 } catch (SchedulerException e) { 21 e.printStackTrace(); 22 } 23 return "start"; 24 } 25 26 @RequestMapping("/info") 27 @ResponseBody 28 public String getQuartzJob(String name, String group) { 29 String info = null; 30 try { 31 info = quartzConfig.getJobInfo(name, group); 32 System.out.println("name="+name); 33 } catch (SchedulerException e) { 34 e.printStackTrace(); 35 } 36 return info; 37 } 38 39 @RequestMapping("/modify") 40 @ResponseBody 41 public boolean modifyQuartzJob(String name, String group, String time) { 42 boolean flag = true; 43 try { 44 flag = quartzConfig.modifyJob(name, group, time); 45 } catch (SchedulerException e) { 46 e.printStackTrace(); 47 } 48 return flag; 49 } 50 51 @RequestMapping(value = "/pause") 52 @ResponseBody 53 public String pauseQuartzJob(String name, String group) { 54 try { 55 quartzConfig.pauseJob(name, group); 56 } catch (SchedulerException e) { 57 e.printStackTrace(); 58 } 59 return "pause "+name; 60 } 61 62 @RequestMapping(value = "/pauseAll") 63 @ResponseBody 64 public String pauseAllQuartzJob() { 65 try { 66 quartzConfig.pauseAllJob(); 67 } catch (SchedulerException e) { 68 e.printStackTrace(); 69 } 70 return "stopAll"; 71 } 72 @RequestMapping(value = "/resumeAllJob") 73 @ResponseBody 74 public String resumeAllJob(){ 75 try { 76 quartzConfig.resumeAllJob(); 77 } catch (SchedulerException e) { 78 e.printStackTrace(); 79 } 80 return "resumeAllJob"; 81 } 82 83 @RequestMapping(value = "/delete") 84 public void deleteJob(String name, String group) { 85 try { 86 quartzConfig.deleteJob(name, group); 87 } catch (SchedulerException e) { 88 e.printStackTrace(); 89 } 90 } 91 92 }
配置controller