spring4+quartz2.x 动态定时任务
- - Java - 编程语言 - ITeye博客 最近看quartz定时任务,看到博客园的一篇文章http://www.cnblogs.com/xrab/p/5850186.html( Spring整合Quartz实现动态定时器),他用的是spring3.0.7,quartz是1.8.6,而我用的是spring4.3.2和quartz2.2.1.
最近看quartz定时任务,看到博客园的一篇文章http://www.cnblogs.com/xrab/p/5850186.html( Spring整合Quartz实现动态定时器),他用的是spring3.0.7,quartz是1.8.6,而我用的是spring4.3.2和quartz2.2.1。
我将作者的源码拷贝下来后,发现由于版本问题,很多地方需要调整,不过大部分内容都是基于原作者的代码上进行修改的,还是得感谢这位博主。
下面将修改后的代码贴上,关于一些说明性的东西可去原作者的博客( Spring整合Quartz实现动态定时器)查看。
由于spring-webmvc依赖spring的一些基础包,所以这里就不用再引入了。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- Quartz依赖 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
package com.axujie.task.jobs; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorld implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello World...."); } }
注意:里面JobDetail和 Trigger的实现,我看官方文档的时候不仔细,漏了几处重要的地方,所以照着官方文档没敲出来
import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.CronScheduleBuilder.*;
这是导入这几个类的静态方法,之前一直没注意看。。。。
下面是完整的
package com.axujie.task.manager; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.CronScheduleBuilder.*; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.TriggerKey; /** * Quartz调度管理器 * * @author XuJie * @Date 2017年1月21日 */ public class QuartzManager { private static String JOB_GROUP_NAME = "JOB_GROUP_SYSTEM"; private static String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_SYSTEM"; /** * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 * * @param sched * 调度器 * * @param jobName * 任务名 * @param cls * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java */ public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) { try { JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME);// 任务名,任务组,任务执行类 @SuppressWarnings("unchecked") JobDetail jobDetail = newJob(cls).withIdentity(jobKey).build(); TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME);// 触发器 Trigger trigger = newTrigger().withIdentity(triggerKey) .withSchedule(cronSchedule(time)).build();// 触发器时间设定 sched.scheduleJob(jobDetail, trigger); if (!sched.isShutdown()) { sched.start();// 启动 } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 添加一个定时任务 * * @param sched * 调度器 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务 * @param time * 时间设置,参考quartz说明文档 * * @Title: QuartzManager.java */ public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) { try { JobKey jobKey = new JobKey(jobName, jobGroupName); @SuppressWarnings("unchecked") JobDetail jobDetail = newJob(jobClass).withIdentity(jobKey).build(); // 触发器 TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); Trigger trigger = newTrigger().withIdentity(triggerKey) .withSchedule(cronSchedule(time)).build(); sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名) * * @param sched * 调度器 * @param jobName * @param time * * @Title: QuartzManager.java */ @SuppressWarnings("rawtypes") public static void modifyJobTime(Scheduler sched, String jobName, String time) { try { TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); JobDetail jobDetail = sched.getJobDetail(jobKey); Class objJobClass = jobDetail.getJobClass(); removeJob(sched, jobName); System.out.println("修改任务:"+jobName); addJob(sched, jobName, objJobClass, time); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 修改一个任务的触发时间 * * @param sched * 调度器 * * @param sched * 调度器 * @param triggerName * @param triggerGroupName * @param time * * @Title: QuartzManager.java */ public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) { try { TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); if (trigger == null) { return; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { // 修改时间 trigger.getTriggerBuilder().withSchedule(cronSchedule(time)); // 重启触发器 sched.resumeTrigger(triggerKey); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名) * * @param sched * 调度器 * @param jobName * * @Title: QuartzManager.java */ public static void removeJob(Scheduler sched, String jobName) { try { TriggerKey triggerKey = new TriggerKey(jobName, TRIGGER_GROUP_NAME); sched.pauseTrigger(triggerKey);// 停止触发器 sched.unscheduleJob(triggerKey);// 移除触发器 JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); sched.deleteJob(jobKey);// 删除任务 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description: 移除一个任务 * * @param sched * 调度器 * @param jobName * @param jobGroupName * @param triggerName * @param triggerGroupName * * @Title: QuartzManager.java */ public static void removeJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) { try { TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroupName); sched.pauseTrigger(triggerKey);// 停止触发器 sched.unscheduleJob(triggerKey);// 移除触发器 JobKey jobKey = new JobKey(jobName, jobGroupName); sched.deleteJob(jobKey);// 删除任务 } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:启动所有定时任务 * * @param sched * 调度器 * * @Title: QuartzManager.java */ public static void startJobs(Scheduler sched) { try { sched.start(); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Description:关闭所有定时任务 * * @param sched * 调度器 * */ public static void shutdownJobs(Scheduler sched) { try { if (!sched.isShutdown()) { sched.shutdown(); } } catch (Exception e) { throw new RuntimeException(e); } } }
该类定义成public,并且实现了Job接口,在execute方法中定义自已的业务实现就可以了
package com.axujie.task.jobs; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorld implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello World...."); } }
此类中使用Class.forName来动态创建任务类,此类也是参照原文写的,原文地址:( Spring整合Quartz实现动态定时器)
import org.junit.Test; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; import com.axujie.task.manager.QuartzManager; public class QuartzTest { @Test public void quartz() throws ClassNotFoundException { try { SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); Scheduler sche = gSchedulerFactory.getScheduler(); String job_name = "动态任务调度"; System.out.println("【系统启动】开始(每1秒输出一次)..."); String clazz = "com.axujie.task.jobs.HelloWorld"; String cron = "*/10 * * * * ?"; //使用Class.forName动态的创建 QuartzManager.addJob(sche, job_name, Class.forName(clazz), cron); Thread.sleep(3000); System.out.println("【修改时间】开始(每2秒输出一次)..."); QuartzManager.modifyJobTime(sche, job_name, "10/2 * * * * ?"); Thread.sleep(4000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(sche, job_name); System.out.println("【移除定时】成功"); System.out.println("【再次添加定时任务】开始(每10秒输出一次)..."); QuartzManager.addJob(sche, job_name, Class.forName(clazz), "*/10 * * * * ?"); Thread.sleep(30000); System.out.println("【移除定时】开始..."); QuartzManager.removeJob(sche, job_name); System.out.println("【移除定时】成功"); } catch (Exception e) { e.printStackTrace(); } } }
【系统启动】开始(每1秒输出一次)... 【修改时间】开始(每2秒输出一次)... 修改任务:动态任务调度 Hello World.... Hello World.... 【移除定时】开始... 【移除定时】成功 【再次添加定时任务】开始(每10秒输出一次)... Hello World....
大部分都是原文作者( Spring整合Quartz实现动态定时器)的东西,只是略做修改,适用于spring4和quartz2罢了。
一定要注意这几个静态方法。。。。
import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.CronScheduleBuilder.*;