1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 12 13 <!-- 要調用的工做類 --> 14 <bean id="jobclass" class="com.sh.test.dingshi"></bean> 15 16 <!-- 配置方法調用 --> 17 <bean id="conJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 18 <property name="targetObject" ref="jobclass"></property> 19 <property name="targetMethod" value="say"></property> 20 <!-- 是否容許同時運行 --> 21 <property name="concurrent" value="false"></property> 22 </bean> 23 24 <!-- 定時重複執行 --> 25 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 26 <property name="jobDetail" ref="conJob"></property> 27 <property name="cronExpression" > 28 <value>0/5 * * ? * * </value><!-- 配置5秒執行一次 --> 29 </property> 30 </bean> 31 32 <!-- 定時任務配置,定時工廠,用於管理配置的全部定時器,保證其在單獨的線程上運行 --> 33 <bean id="quartzfactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 34 <property name="triggers"> 35 <list> 36 <ref local="cronTrigger"/><!-- 註冊開關,local爲上面配置的--> 37 </list> 38 </property> 39 </bean> 40 41 </beans>
quartz-1.8.5.jarjava
1 package com.sh.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.springframework.stereotype.Service; 7 8 @Service("dingshi") 9 public class dingshi { 10 public void say(){ 11 String format = "yyyy-MM-dd HH:mm:ss"; 12 SimpleDateFormat sdf = new SimpleDateFormat(format); 13 System.out.println("定時循環執行:"+sdf.format(new Date())); 14 } 15 }
這配置有兩種方式web
1.經過在任務類(dingshi.java)中配置註解@Service("dingshi")spring
2.在配置文件(spring-dingshi.xml)中第18行配置ref="dingshi",無需第14行spring-mvc
1.在配置文件(spring-dingshi.xml)中配置第14行內容,在第18行ref=「id」配置18行的idmvc
1 package com.sh.test; 2 3 import org.quartz.CronTrigger; 4 import org.quartz.JobDetail; 5 import org.quartz.Scheduler; 6 import org.quartz.SchedulerFactory; 7 import org.quartz.impl.StdSchedulerFactory; 8 9 /** 10 * @Description: 定時任務管理類 11 * 12 * @ClassName: QuartzManager 13 * @Copyright: Copyright (c) 2015 14 * 15 * @author SH 16 * @version V2.0 17 */ 18 public class QuartzManager { 19 private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); 20 private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME"; 21 private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME"; 22 23 /** 24 * @Description: 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名 25 * 26 * @param jobName 27 * 任務名 28 * @param cls 29 * 任務 30 * @param time 31 * 時間設置,參考quartz說明文檔 32 * 33 * @Title: QuartzManager.java 34 * @Copyright: Copyright (c) 2015 35 * 36 * @author SH 37 * @version V2.0 38 */ 39 @SuppressWarnings("unchecked") 40 public static void addJob(String jobName, Class cls, String time) { 41 try { 42 Scheduler sched = gSchedulerFactory.getScheduler(); 43 JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任務名,任務組,任務執行類 44 // 觸發器 45 CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 觸發器名,觸發器組 46 trigger.setCronExpression(time);// 觸發器時間設定 47 sched.scheduleJob(jobDetail, trigger); 48 // 啓動 49 if (!sched.isShutdown()) { 50 sched.start(); 51 } 52 } catch (Exception e) { 53 throw new RuntimeException(e); 54 } 55 } 56 57 /** 58 * @Description: 添加一個定時任務 59 * 60 * @param jobName 61 * 任務名 62 * @param jobGroupName 63 * 任務組名 64 * @param triggerName 65 * 觸發器名 66 * @param triggerGroupName 67 * 觸發器組名 68 * @param jobClass 69 * 任務 70 * @param time 71 * 時間設置,參考quartz說明文檔 72 * 73 * @Title: QuartzManager.java 74 * @Copyright: Copyright (c) 2015 75 * 76 * @author SH 77 * @version V2.0 78 */ 79 @SuppressWarnings("unchecked") 80 public static void addJob(String jobName, String jobGroupName, 81 String triggerName, String triggerGroupName, Class jobClass, 82 String time) { 83 try { 84 Scheduler sched = gSchedulerFactory.getScheduler(); 85 JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任務名,任務組,任務執行類 86 // 觸發器 87 CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 觸發器名,觸發器組 88 trigger.setCronExpression(time);// 觸發器時間設定 89 sched.scheduleJob(jobDetail, trigger); 90 } catch (Exception e) { 91 throw new RuntimeException(e); 92 } 93 } 94 95 /** 96 * @Description: 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名) 97 * 98 * @param jobName 99 * @param time 100 * 101 * @Title: QuartzManager.java 102 * @Copyright: Copyright (c) 2015 103 * 104 * @author SH 105 * @version V2.0 106 */ 107 @SuppressWarnings("unchecked") 108 public static void modifyJobTime(String jobName, String time) { 109 try { 110 Scheduler sched = gSchedulerFactory.getScheduler(); 111 CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME); 112 if (trigger == null) { 113 return; 114 } 115 String oldTime = trigger.getCronExpression(); 116 if (!oldTime.equalsIgnoreCase(time)) { 117 JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME); 118 Class objJobClass = jobDetail.getJobClass(); 119 removeJob(jobName); 120 addJob(jobName, objJobClass, time); 121 } 122 } catch (Exception e) { 123 throw new RuntimeException(e); 124 } 125 } 126 127 /** 128 * @Description: 修改一個任務的觸發時間 129 * 130 * @param triggerName 131 * @param triggerGroupName 132 * @param time 133 * 134 * @Title: QuartzManager.java 135 * @Copyright: Copyright (c) 2015 136 * 137 * @author SH 138 * @version V2.0 139 */ 140 public static void modifyJobTime(String triggerName, 141 String triggerGroupName, String time) { 142 try { 143 Scheduler sched = gSchedulerFactory.getScheduler(); 144 CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName); 145 if (trigger == null) { 146 return; 147 } 148 String oldTime = trigger.getCronExpression(); 149 if (!oldTime.equalsIgnoreCase(time)) { 150 CronTrigger ct = (CronTrigger) trigger; 151 // 修改時間 152 ct.setCronExpression(time); 153 // 重啓觸發器 154 sched.resumeTrigger(triggerName, triggerGroupName); 155 } 156 } catch (Exception e) { 157 throw new RuntimeException(e); 158 } 159 } 160 161 /** 162 * @Description: 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名) 163 * 164 * @param jobName 165 * 166 * @Title: QuartzManager.java 167 * @Copyright: Copyright (c) 2015 168 * 169 * @author SH 170 * @version V2.0 171 */ 172 public static void removeJob(String jobName) { 173 try { 174 Scheduler sched = gSchedulerFactory.getScheduler(); 175 sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 中止觸發器 176 sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除觸發器 177 sched.deleteJob(jobName, JOB_GROUP_NAME);// 刪除任務 178 } catch (Exception e) { 179 throw new RuntimeException(e); 180 } 181 } 182 183 /** 184 * @Description: 移除一個任務 185 * 186 * @param jobName 187 * @param jobGroupName 188 * @param triggerName 189 * @param triggerGroupName 190 * 191 * @Title: QuartzManager.java 192 * @Copyright: Copyright (c) 2015 193 * 194 * @author SH 195 * @version V2.0 196 */ 197 public static void removeJob(String jobName, String jobGroupName, 198 String triggerName, String triggerGroupName) { 199 try { 200 Scheduler sched = gSchedulerFactory.getScheduler(); 201 sched.pauseTrigger(triggerName, triggerGroupName);// 中止觸發器 202 sched.unscheduleJob(triggerName, triggerGroupName);// 移除觸發器 203 sched.deleteJob(jobName, jobGroupName);// 刪除任務 204 } catch (Exception e) { 205 throw new RuntimeException(e); 206 } 207 } 208 209 /** 210 * @Description:啓動全部定時任務 211 * 212 * 213 * @Title: QuartzManager.java 214 * @Copyright: Copyright (c) 2015 215 * 216 * @author SH 217 * @version V2.0 218 */ 219 public static void startJobs() { 220 try { 221 Scheduler sched = gSchedulerFactory.getScheduler(); 222 sched.start(); 223 } catch (Exception e) { 224 throw new RuntimeException(e); 225 } 226 } 227 228 /** 229 * @Description:關閉全部定時任務 230 * 231 * 232 * @Title: QuartzManager.java 233 * @Copyright: Copyright (c) 2015 234 * 235 * @author SH 236 * @version V2.0 237 */ 238 public static void shutdownJobs() { 239 try { 240 Scheduler sched = gSchedulerFactory.getScheduler(); 241 if (!sched.isShutdown()) { 242 sched.shutdown(); 243 } 244 } catch (Exception e) { 245 throw new RuntimeException(e); 246 } 247 } 248 }
1 package com.sh.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import org.quartz.Job; 7 import org.quartz.JobExecutionContext; 8 import org.quartz.JobExecutionException; 9 10 /** 11 * @Description: 任務執行類 12 * 13 * @ClassName: QuartzJob 14 * @Copyright: Copyright (c) 2015 15 * 16 * @author SH 17 * @version V2.0 18 */ 19 public class QuartzJob implements Job { 20 21 public void execute(JobExecutionContext arg0) throws JobExecutionException { 22 String name = arg0.getTrigger().getName();//得到當前執行任務名 23 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); 24 } 25 }
1 package com.sh.test; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 10 11 /** 12 * @Description: 測試類 13 * 14 * @ClassName: QuartzTest 15 * @Copyright: Copyright (c) 2014 16 * 17 * @author SH 18 * @version V2.0 19 */ 20 @Controller 21 public class QuartzTest { 22 public static void main(String[] args) { 23 try { 24 String job_name = "動態任務調度"; 25 System.out.println("【系統啓動】開始(每1秒輸出一次)..."); 26 QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?"); 27 28 Thread.sleep(5000); 29 System.out.println("【修改時間】開始(每2秒輸出一次)..."); 30 QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?"); 31 Thread.sleep(6000); 32 System.out.println("【移除定時】開始..."); 33 QuartzManager.removeJob(job_name); 34 System.out.println("【移除定時】成功"); 35 36 System.out.println("【再次添加定時任務】開始(每10秒輸出一次)..."); 37 QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?"); 38 Thread.sleep(60000); 39 System.out.println("【移除定時】開始..."); 40 QuartzManager.removeJob(job_name); 41 System.out.println("【移除定時】成功"); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 } 46 String job_name = "動態任務調度"; 47 @RequestMapping(value="/add",method=RequestMethod.GET) 48 public void timmertest(HttpServletRequest request,HttpServletResponse response){ 49 System.out.println("【系統啓動】開始(每1秒輸出一次)..."); 50 QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?"); 51 } 52 @RequestMapping(value="/update",method=RequestMethod.GET) 53 public void timmertest1(HttpServletRequest request,HttpServletResponse response){ 54 System.out.println("【修改時間】開始(每2秒輸出一次)..."); 55 QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?"); 56 } 57 @RequestMapping(value="/del",method=RequestMethod.GET) 58 public void timmertest2(HttpServletRequest request,HttpServletResponse response){ 59 QuartzManager.removeJob(job_name); 60 System.out.println("【移除定時】成功"); 61 } 62 }