需要定時執行一些計劃(定時更新等),這樣的計劃稱之為計劃任務
Spring抽象封裝了java提供的Timer與TimerTask類
也可以使用擁有更多任務計劃功能的Quartz
二、TimerTask2.1、繼承TimerTask類重寫run方法

實現類
package com.pb.task.timertask;import java.util.Iterator;import java.util.List;import java.util.TimerTask;public class BatchUpdate extends TimerTask { //存放SQL private List commons; @Override public void run() { //輸出語句 for(Iterator it=commons.iterator();it.hasNext();){ System.out.println(it.next()); } System.out.println("timertask批量更新完畢"); } public void setCommons(List commons) { this.commons = commons; } }配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="batch_update" class="com.pb.task.timertask.BatchUpdate"><property name="commons"><list><value>update personn set onduty="出勤" where date=${ontime}</value><value>update personn set onduty="缺勤" where date=${miss}</value><value>update personn set onduty="遲到" where date=${late}</value><value>update personn set onduty="早退" where date=${early}</value></list></property></bean><!-- 配置任務調度的類 --><bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask" ref="batch_update"/><!--啟動任務后多久開始執行 --><property name="delay"><value>1000</value></property><!--第一次記動后后多久執行一次 2秒重復一次--><property name="period"><value>2000</value></property></bean><!--啟動任務 --><bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"><!--任務調度類 --><property name="scheduledTimerTasks"><list><ref local="update_schuleTask"/></list></property></bean></beans>
測試類只需要調用
applicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");2.2、使用Spring提供的MethodInvokingTimerTaskFactoryBean定義計劃任務


package com.pb.task.timertask;import java.util.Iterator;import java.util.List;import java.util.TimerTask;public class MethodInvokingBatchUpdate { //存放SQL private List commons; public void run() { //輸出語句 for(Iterator it=commons.iterator();it.hasNext();){ System.out.println(it.next()); } System.out.println("MethodInvoking批量更新完畢"); } public void setCommons(List commons) { this.commons = commons; } }配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="batch_update" class="com.pb.task.timertask.MethodInvokingBatchUpdate"><property name="commons"><list><value>update personn set onduty="出勤" where date=${ontime}</value><value>update personn set onduty="缺勤" where date=${miss}</value><value>update personn set onduty="遲到" where date=${late}</value><value>update personn set onduty="早退" where date=${early}</value></list></property></bean><!--配置方法的調用類 --><bean id="methodInvoking" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"><property name="targetObject" ref="batch_update"></property><property name="targetMethod" value="run"/></bean><!-- 配置任務調度的類 --><bean id="update_schuleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask" ref="methodInvoking"/><!--啟動任務后多久開始執行 --><property name="delay"><value>1000</value></property><!--第一次記動后后多久執行一次 2秒重復一次--><property name="period"><value>2000</value></property></bean><!--啟動任務 --><bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"><!--任務調度類 --><property name="scheduledTimerTasks"><list><ref local="update_schuleTask"/></list></property></bean></beans>三、QuartzJobBean實現



傳統QuartzJob
需要jta.jar 和quartz-all-1.6.0.jar2個jar包
package com.pb.quartz.job;import java.util.Date;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;/** * 傳統的使用Quartz * */public class QuartzUpdate extends QuartzJobBean { private String command; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println(new Date()+":"+"傳統Quartz任務被高度"); for (int i = 1; i <= 10; i++) { System.out.println("命令:"+command+"第"+i+"次被讞用"); } System.out.println(new Date()+"傳統Quartz調度完成"); } public void setCommand(String command) { this.command = command; } }配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--配置傳統方式創建Quartz --><bean id="t_quartz" class="org.springframework.scheduling.quartz.JobDetailBean"><!--配置哪個類 --><property name="jobClass"><!--關聯 --><value>com.pb.quartz.job.QuartzUpdate</value></property><!-- 賦值--><property name="jobDataAsMap"><map><entry key="command"><value>更新</value></entry></map></property></bean><!--配置觸發器簡單觸發器 --><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"><property name="jobDetail" ref="t_quartz"/><!--啟動時間 --><property name="startDelay"><value>1000</value></property><!--間隔 --><property name="repeatInterval"><value>2000</value></property></bean><!--啟動任務 --><bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="simpleTrigger"/></list></property></bean></beans>
使用MethodInvoking方式
package com.pb.quartz.methodinvoking.update;public class UpdateQuartzJob { private String command; public void show(){ System.out.println("MethodInvoking方式調度開始"+command); for (int i = 1; i <=10; i++) { System.out.println("命令:"+command+"第"+i+"次調用"); } System.out.println("MethodInvoking方式調度結束"+command); } public String getCommand() { return command; } public void setCommand(String command) { this.command = command; } }配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 執行的Bean --><bean id="updateQuartzJob" class="com.pb.quartz.methodinvoking.update.UpdateQuartzJob"><property name="command"><value>Spring新型更新或者插入</value></property></bean><!-- 新的通過方式調用的 --><bean id="methodInvoking" class=" org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!--哪個類要被調用 --><property name="targetObject" ref="updateQuartzJob"/><!--哪個方法要執行 --><property name="targetMethod" value="show"/></bean><!--配置觸發器定時觸發器 --><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail" ref="methodInvoking"/><!--表達式 --><property name="cronExpression"><!-- 每天23:17:01秒調用 --><value>02 17 23 * * ?</value></property></bean><!--啟動任 --><bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><!--觸發器名字 --><property name="triggers"><list><ref local="cronTrigger" /></list></property></bean></beans>
新聞熱點
疑難解答