一、任務調度java
第一種狀況(繼承TimerTask)spring
BatchUpdate.javaapp
package com.ljb.timer.timertask; import java.util.Iterator; import java.util.List; import java.util.TimerTask; /** * 使用繼承的方式建立定時任務 * @author Administrator * */ public class BatchUpdate extends TimerTask { private List commands; public void setCommands(List commands) { this.commands = commands; } @Override public void run() { for (Iterator it = commands.iterator();it.hasNext();) { System.out.println(it.next()); } System.out.println("批量更新執行完畢!"); } }
extendsTimerTask_applicationContext.xmlide
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 建立任務 --> <bean id="batch_update" class="com.ljb.timer.timertask.BatchUpdate"> <property name="commands"> <list> <value>update person set onduty="出勤" where date=${ontime}</value> <value>update person set onduty="缺勤" where date=${miss}</value> <value>update person set onduty="遲到" where date=${late}</value> </list> </property> </bean> <!-- 配置執行任務 --> <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="batch_update"></property> <property name="delay" value="1000"></property> <property name="period" value="2000"></property> </bean> <!-- 啓動任務 --> <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="scheduledTimerTask"/> </list> </property> </bean> </beans>
ExtendsTest.javathis
package com.ljb.timer.timertask; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExtendsTest { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("extendsTimerTask_applicationContext.xml"); } }
第二種狀況(在spring中配置MethodInvokingTimerTaskFactoryBean)spa
BatchUpdateTwo.javacode
package com.ljb.timer.timertask; import java.util.Iterator; import java.util.List; import java.util.TimerTask; /** * 使用MethodInvokingTimerFactoryBean建立定時任務 * @author Administrator * */ public class BatchUpdateTwo{ private List commands; public void setCommands(List commands) { this.commands = commands; } public void run() { for (Iterator it = commands.iterator();it.hasNext();) { System.out.println(it.next()); } System.out.println("methodInvoking批量更新執行完畢!"); } }
methodInvokingTimerTaskFactoryBean_applicationContext.xmlxml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="batchUpdate2" class="com.ljb.timer.timertask.BatchUpdateTwo"> <property name="commands"> <list> <value>update person set onduty="出勤" where date=${ontime}</value> <value>update person set onduty="缺勤" where date=${miss}</value> <value>update person set onduty="遲到" where date=${late}</value> </list> </property> </bean> <bean id="methodInvokingTimerTaskFactoryBean" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="batchUpdate2"></property> <property name="targetMethod" value="run"></property> </bean> <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="methodInvokingTimerTaskFactoryBean"></property> <property name="delay" value="1000"></property> <property name="period" value="2000"></property> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="scheduledTimerTask"/> </list> </property> </bean> </beans>
MethodInvokingTest.java繼承
package com.ljb.timer.timertask; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MethodInvokingTest { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("methodInvokingTimerTaskFactoryBean_applicationContext.xml"); } }
二、使用Quartzget
樣例:
第一種狀況(傳統quartz任務,繼承QuartzJobBean)
QuartzJob.java
package com.ljb.quartz.jobdetail; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class QuartzJob extends QuartzJobBean { private String command; public void setCommand(String command) { this.command = command; } @Override protected void executeInternal(JobExecutionContext arg0) 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任務執行完畢"); } }
quartzJob_applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 傳統方式建立quartz任務bean --> <bean id="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.ljb.quartz.jobdetail.QuartzJob</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="quartzJob"></property> <property name="startDelay" value="1000"></property> <property name="repeatInterval" value="2000"></property> </bean> <!-- 啓動任務 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="simpleTrigger"/> </list> </property> </bean> </beans>
RunQuartzJob.java
package com.ljb.quartz.jobdetail; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class RunQuartzJob { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("quartzJob_applicationContext.xml"); } }
注:須要的jar包(commons-collections-3.1.jar;quartz-all-1.6.0.jar;jta-1.1.jar)
執行結果:
Mon Jul 06 13:43:51 CST 2015:傳統quartz任務被調用
命令更新被第1次被調用
命令更新被第2次被調用
命令更新被第3次被調用
命令更新被第4次被調用
命令更新被第5次被調用
命令更新被第6次被調用
命令更新被第7次被調用
命令更新被第8次被調用
命令更新被第9次被調用
命令更新被第10次被調用
Mon Jul 06 13:43:51 CST 2015:傳統quartz任務執行完畢
Mon Jul 06 13:43:53 CST 2015:傳統quartz任務被調用
命令更新被第1次被調用
命令更新被第2次被調用
命令更新被第3次被調用
命令更新被第4次被調用
命令更新被第5次被調用
命令更新被第6次被調用
命令更新被第7次被調用
命令更新被第8次被調用
命令更新被第9次被調用
命令更新被第10次被調用
Mon Jul 06 13:43:53 CST 2015:傳統quartz任務執行完畢
第二種狀況(在spring中配置MethodInvokingJobDetailFactoryBean)
QuartzJob2.java
package com.ljb.quartz.jobdetail; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class QuartzJob2{ private String command; public void setCommand(String command) { this.command = command; } public void test() throws JobExecutionException { System.out.println(new Date() + ":使用methodInvoking傳統quartz任務被調用"); for (int i=1; i <=10 ; i++) { System.out.println("命令"+ command + "被第"+i+"次被調用"); } System.out.println(new Date() + ":使用methodInvoking傳統quartz任務執行完畢"); } }
quartzJob_applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 傳統方式建立quartz任務bean --> <bean id="quartzJob" class="com.ljb.quartz.jobdetail.QuartzJob2"> <property name="command" value="更新"> </property> </bean> <!-- 配置通過方法調用任務器 --> <bean id="methodInvoking" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzJob"></property> <property name="targetMethod" value="test"></property> </bean> <!-- 簡單觸發器 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="methodInvoking"></property> <property name="startDelay" value="1000"></property> <property name="repeatInterval" value="2000"></property> </bean> <!-- 啓動任務 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="simpleTrigger"/> </list> </property> </bean> </beans>
RunQuartzJob.java
package com.ljb.quartz.jobdetail; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class RunQuartzJob { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("quartzJob_applicationContext.xml"); } }
執行結果:
Mon Jul 06 14:19:45 CST 2015:使用methodInvoking傳統quartz任務被調用 命令更新被第1次被調用 命令更新被第2次被調用 命令更新被第3次被調用 命令更新被第4次被調用 命令更新被第5次被調用 命令更新被第6次被調用 命令更新被第7次被調用 命令更新被第8次被調用 命令更新被第9次被調用 命令更新被第10次被調用 Mon Jul 06 14:19:45 CST 2015:使用methodInvoking傳統quartz任務執行完畢 Mon Jul 06 14:19:47 CST 2015:使用methodInvoking傳統quartz任務被調用 命令更新被第1次被調用 命令更新被第2次被調用 命令更新被第3次被調用 命令更新被第4次被調用 命令更新被第5次被調用 命令更新被第6次被調用 命令更新被第7次被調用 命令更新被第8次被調用 命令更新被第9次被調用 命令更新被第10次被調用 Mon Jul 06 14:19:47 CST 2015:使用methodInvoking傳統quartz任務執行完畢
第三種狀況(使用cronTrigger觸發器)
quartzJob_applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 傳統方式建立quartz任務bean --> <bean id="quartzJob" class="com.ljb.quartz.jobdetail.QuartzJob2"> <property name="command" value="更新"> </property> </bean> <!-- 配置通過方法調用任務器 --> <bean id="methodInvoking" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzJob"></property> <property name="targetMethod" value="test"></property> </bean> <!-- 簡單觸發器 --> <!-- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="methodInvoking"></property> <property name="startDelay" value="1000"></property> <property name="repeatInterval" value="2000"></property> </bean> --> <!-- 表達式觸發器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="methodInvoking"></property> <property name="cronExpression"> <value>00 32 16 * * ?</value> </property> </bean> <!-- 啓動任務 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger"/> </list> </property> </bean> </beans>
執行結果:
Mon Jul 06 16:32:00 CST 2015:使用(表達式觸發器)methodInvoking傳統quartz任務被調用命令更新被第1次被調用命令更新被第2次被調用命令更新被第3次被調用命令更新被第4次被調用命令更新被第5次被調用命令更新被第6次被調用命令更新被第7次被調用命令更新被第8次被調用命令更新被第9次被調用命令更新被第10次被調用Mon Jul 06 16:32:00 CST 2015:使用(表達式觸發器)methodInvoking傳統quartz任務執行完畢