进来项目中用到了quartz作为调度框架,在搭建框架的时候添加了一个调度模块,现将代码分享出来,给有需要的朋友参考。这个任务调度可以作为一个单独的模块去开发,所以并不会改变原有的架构,话不多说,直接上代码。
1.添加Spring的事件监听,IOC容器启动完成后初始化任务群,添加任务也主要是在这个类里面添加
1 package com.sunyard.main; 2 3 import org.apache.logging.log4j.LogManager; 4 import org.apache.logging.log4j.Logger; 5 import org.springframework.context.ApplicationListener; 6 import org.springframework.context.event.ContextRefreshedEvent; 7 8 import com.sunyard.cache.SystemCache; 9 import com.sunyard.constans.Constants; 10 import com.sunyard.quartz.WKTriggerFactory; 11 import com.sunyard.quartz.jobs.TestJob; 12 13 public class InitDataAfterSpring implements ApplicationListener<ContextRefreshedEvent>{ 14 15 private static final Logger logger = LogManager.getLogger(Constants.LOGNAME); 16 17 @Override 18 public void onApplicationEvent(ContextRefreshedEvent event) { 19 if(event.getApplicationContext().getParent() == null){ 20 //初始化测试定时任务 21 TestJob job = (TestJob) SystemCache.getBean("testJob"); 22 /** 23 * job 需要执行的任务 24 * execute 任务中需要执行的方法 25 * 0/10 * * * * ? 执行任务的时间,具体可以参考我有一篇 26 */ 27 WKTriggerFactory.newCronTrigger(job, "execute", 28 "0/10 * * * * ?"); 29 } 30 } 31 }
2.任务调度工厂
1 package com.sunyard.quartz; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.apache.logging.log4j.LogManager; 7 import org.apache.logging.log4j.Logger; 8 import org.quartz.Trigger; 9 import org.springframework.scheduling.quartz.CronTriggerFactoryBean; 10 import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; 11 import org.springframework.scheduling.quartz.SchedulerFactoryBean; 12 13 import com.sunyard.constans.Constants; 14 import com.sunyard.quartz.jobs.WKJob; 15 16 public class WKTriggerFactory { 17 18 private static final Logger logger = LogManager.getLogger(Constants.LOGNAME); 19 private static final String OPERATER_NAME = "【构建触发器】"; 20 21 private static Map<String,SchedulerFactoryBean> schedulerFactoryMaps = new HashMap<String,SchedulerFactoryBean>(); 22 private WKTriggerFactory(){ 23 } 24 25 public static void newCronTrigger(WKJob wkJob,String invokeMethod, String croExpression){ 26 try { 27 //重复的任务,做覆盖操作。 28 if(schedulerFactoryMaps.containsKey(wkJob.getKey())){ 29 removeScheduler(wkJob.getKey()); 30 } 31 MethodInvokingJobDetailFactoryBean jobDetailFacroty = new MethodInvokingJobDetailFactoryBean(); 32 jobDetailFacroty.setTargetObject(wkJob); 33 jobDetailFacroty.setTargetMethod(invokeMethod); 34 jobDetailFacroty.setName(wkJob.getKey()); 35 jobDetailFacroty.setConcurrent(true); 36 jobDetailFacroty.afterPropertiesSet(); 37 38 CronTriggerFactoryBean triggerfactory = new CronTriggerFactoryBean(); 39 triggerfactory.setJobDetail(jobDetailFacroty.getObject()); 40 triggerfactory.setCronExpression(croExpression); 41 triggerfactory.setName(wkJob.getKey()); 42 triggerfactory.afterPropertiesSet(); 43 44 SchedulerFactoryBean schdulerFactory = new SchedulerFactoryBean(); 45 schdulerFactory.setTriggers(new Trigger[]{triggerfactory.getObject()}); 46 schdulerFactory.afterPropertiesSet(); 47 schdulerFactory.start(); 48 49 schedulerFactoryMaps.put(wkJob.getKey(), schdulerFactory); 50 } catch (Exception e) { 51 logger.error(OPERATER_NAME, e); 52 } 53 } 54 55 public static void removeScheduler(String key){ 56 try { 57 SchedulerFactoryBean schdulerFactory = schedulerFactoryMaps.remove(key); 58 if(schdulerFactory != null){ 59 schdulerFactory.destroy(); 60 }else{ 61 logger.info(OPERATER_NAME + "定时任务已被移除。"); 62 } 63 } catch (Exception e) { 64 logger.error(OPERATER_NAME, e); 65 } 66 } 67 68 }
3.抽象任务类
1 package com.sunyard.quartz.jobs; 2 3 import java.util.UUID; 4 5 import org.apache.logging.log4j.LogManager; 6 import org.apache.logging.log4j.Logger; 7 8 import com.sunyard.constans.Constants; 9 10 public abstract class WKJob { 11 12 //private static final Logger logger = LogManager.getLogger(Constants.LOGNAME); 13 //private static final String OPERATER_NAME = "【触发器】"; 14 15 protected String key = UUID.randomUUID().toString(); 16 17 public void execute(){ 18 doAction(); 19 //logger.info(OPERATER_NAME + "执行完毕,移除定时任务。"); 20 //WKTriggerFactory.removeScheduler(key); 21 } 22 23 public abstract void doAction(); 24 25 public String getKey() { 26 return key; 27 } 28 29 public void setKey(String key) { 30 this.key = key; 31 } 32 33 }
4.具体任务类
1 package com.sunyard.quartz.jobs; 2 3 import org.apache.logging.log4j.LogManager; 4 import org.apache.logging.log4j.Logger; 5 import org.springframework.context.annotation.Scope; 6 import org.springframework.stereotype.Component; 7 8 import com.sunyard.constans.Constants; 9 10 @Component("testJob") 11 @Scope(value = "prototype") 12 public class TestJob extends WKJob{ 13 14 private static final Logger logger = LogManager.getLogger(Constants.LOGNAME); 15 private static final String OPERATER_NAME = "【测试定时器】"; 16 17 @Override 18 public void doAction() { 19 try { 20 System.out.println("测试定时任务"); 21 } catch (Exception e) { 22 logger.error(OPERATER_NAME, e); 23 } 24 logger.info(OPERATER_NAME + "结束了..."); 25 } 26 }
启动服务器观看效果:
1 测试定时任务 2 2017-07-25 17:38:50 GMT+08:00 INFO com.sunyard.quartz.jobs.TestJob 24 doAction - 【测试定时器】结束了... 3 测试定时任务 4 2017-07-25 17:39:00 GMT+08:00 INFO com.sunyard.quartz.jobs.TestJob 24 doAction - 【测试定时器】结束了... 5 测试定时任务 6 2017-07-25 17:39:10 GMT+08:00 INFO com.sunyard.quartz.jobs.TestJob 24 doAction - 【测试定时器】结束了... 7 测试定时任务 8 2017-07-25 17:39:20 GMT+08:00 INFO com.sunyard.quartz.jobs.TestJob 24 doAction - 【测试定时器】结束了... 9 测试定时任务 10 2017-07-25 17:39:30 GMT+08:00 INFO com.sunyard.quartz.jobs.TestJob 24 doAction - 【测试定时器】结束了...
我设置的是10秒钟执行一次。
最后强调一下,我的spring版本是3.1.0 以及quartz-2.2.1.jar 如果遇到功能失效或者其他的问题,应当有限考虑jar包的版本问题。还有Spring的IOC容器中必须配置第一步操作的中的监听器类,如果使用的是
SpringMvc框架,应当放在SpringMvc的容器中。
1 <bean class="com.sunyard.quartz.InitDataAfterSpring"/>
如果有问题可以加我QQ:774346810