關於在Spring中的任務計劃的使用
我今天結合Spring技術手冊中的內容,總共總結了5個方面:
1:使用最簡單的任務計劃,就是繼承java.util.TimerTask類,最關鍵的當然是配置beans-config.xml
檔案了,因為我們使用的是spring來管理任務計劃。
繼承TimerTask類的寫法如下:
檔案了,因為我們使用的是spring來管理任務計劃。
繼承TimerTask類的寫法如下:
package com;
import java.util.TimerTask;
public class DemoTask extends TimerTask {
public void run() {
System.out.println(“任務程式啟動,這時我的第一個任務程式……”);
}
}
import java.util.TimerTask;
public class DemoTask extends TimerTask {
public void run() {
System.out.println(“任務程式啟動,這時我的第一個任務程式……”);
}
}
beans-config.xml的寫法如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“[url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”demoTask” class=”com.DemoTask”/>
<bean id=”scheduledTimerTask” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
<!–這裡定義定時任務的物件的位置–>
<property name=”timerTask”>
<ref bean=”demoTask”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”period”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”delay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.timer.TimerFactoryBean”>
<property name=”scheduledTimerTasks”>
<list>
<ref bean=”scheduledTimerTask”/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“[url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”demoTask” class=”com.DemoTask”/>
<bean id=”scheduledTimerTask” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
<!–這裡定義定時任務的物件的位置–>
<property name=”timerTask”>
<ref bean=”demoTask”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”period”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”delay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.timer.TimerFactoryBean”>
<property name=”scheduledTimerTasks”>
<list>
<ref bean=”scheduledTimerTask”/>
</list>
</property>
</bean>
</beans>
啟動程式的寫法:
package com;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.context .support.FileSystemXmlApplicationContext;
public class TimerTaskDemo {
/**
* @author sunny 這個程式用於測試spring框架中的任務管理,這個一個最簡單地實現定時任務的方式
* @param args
*/
public static void main(String[] args) throws Exception{
new FileSystemXmlApplicationContext(“beans-config.xml”);
System.out.println(“啟動任務…”);
System.out.println(“請輸入exit關閉程式…..”);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
if(reader.readLine().equals(“exit”)){
System.exit(0);
}
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.context .support.FileSystemXmlApplicationContext;
public class TimerTaskDemo {
/**
* @author sunny 這個程式用於測試spring框架中的任務管理,這個一個最簡單地實現定時任務的方式
* @param args
*/
public static void main(String[] args) throws Exception{
new FileSystemXmlApplicationContext(“beans-config.xml”);
System.out.println(“啟動任務…”);
System.out.println(“請輸入exit關閉程式…..”);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
if(reader.readLine().equals(“exit”)){
System.exit(0);
}
}
}
}
2:當我們在程式中不能繼承TimerTask類了<wbr>,我們應該怎樣寫呢?那我們就需要在程式中使用spring提供的<wbr>“org.springframework.scheduling<wbr>.timer.MethodInvokingTimerTaskF<wbr>actoryBean”類來進行處理了。不同的地方就是
xml的配置檔案的寫法,程式中需要指定執行哪一個類的哪一個方法<wbr>:
xml的配置檔案的寫法,程式中需要指定執行哪一個類的哪一個方法
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”demoTask” class=”com.DemoTask”/>
<bean id=”timerTaskBean” class=”org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean”>
<property name=”targetObject”>
<ref bean=”demoTask”/>
</property>
<property name=”targetMethod”>
<value>run</value>
</property>
</bean>
<bean id=”scheduledTimerTask” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
<!–這裡定義定時任務的物件的位置–>
<property name=”timerTask”>
<ref bean=”timerTaskBean”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”period”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”delay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.timer.TimerFactoryBean”>
<property name=”scheduledTimerTasks”>
<list>
<ref bean=”scheduledTimerTask”/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”demoTask” class=”com.DemoTask”/>
<bean id=”timerTaskBean” class=”org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean”>
<property name=”targetObject”>
<ref bean=”demoTask”/>
</property>
<property name=”targetMethod”>
<value>run</value>
</property>
</bean>
<bean id=”scheduledTimerTask” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
<!–這裡定義定時任務的物件的位置–>
<property name=”timerTask”>
<ref bean=”timerTaskBean”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”period”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”delay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.timer.TimerFactoryBean”>
<property name=”scheduledTimerTasks”>
<list>
<ref bean=”scheduledTimerTask”/>
</list>
</property>
</bean>
</beans>
3A:由於jdk標準api提供的Timer功能有限<wbr>,只能指定任務與任務之間的週期,而無法指定某個特定的時間內執行<wbr>某項任務,所以現在
推薦使用Quartz來解決這樣的問題。接下來的這個程式仍然是解<wbr>決週期問題的,要循序漸進。。。
QuartzJobBean 類用於重複呼叫JobData 類的方法執行getData()方法。QuartzJobBean 必須繼承QuartzJobBean
推薦使用Quartz來解決這樣的問題。接下來的這個程式仍然是解
QuartzJobBean 類用於重複呼叫JobData 類的方法執行getData()方法。QuartzJobBean 必須繼承QuartzJobBean
package com;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class DemoJob extends QuartzJobBean {
private JobData jobData;
protected void executeInternal(JobExecutionContext arg0) {
System.out.println(jobData.getData() + ” 第一個已經被執行了!!”);
}
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
}
package com;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobData {
public String getData() {
SimpleDateFormat ddd = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
return “Data from “ + ddd.format(new Date());
}
}
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class DemoJob extends QuartzJobBean {
private JobData jobData;
protected void executeInternal(JobExecutionContext arg0) {
System.out.println(jobData.getData() + ” 第一個已經被執行了!!”);
}
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
}
package com;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobData {
public String getData() {
SimpleDateFormat ddd = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
return “Data from “ + ddd.format(new Date());
}
}
關鍵在於beans-config.xml配置檔案的寫法
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
” [url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.JobDetailBean”>
<property name=”jobClass”>
<value>com.DemoJob</value>
</property>
<property name=”jobDataAsMap”>
<map>
<entry key=”jobData”>
<ref bean=”someData”/>
</entry>
</map>
</property>
</bean>
<bean id=”simpleTriggerBean” class=”org.springframework.scheduling.quartz.SimpleTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”repeatInterval”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”startDelay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”simpleTriggerBean”/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
” [url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.JobDetailBean”>
<property name=”jobClass”>
<value>com.DemoJob</value>
</property>
<property name=”jobDataAsMap”>
<map>
<entry key=”jobData”>
<ref bean=”someData”/>
</entry>
</map>
</property>
</bean>
<bean id=”simpleTriggerBean” class=”org.springframework.scheduling.quartz.SimpleTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每六秒鐘程式執行一次–>
<property name=”repeatInterval”>
<value>6000</value>
</property>
<!–這裡定義程式啟動兩秒鐘後開始執行–>
<property name=”startDelay”>
<value>2000</value>
</property>
</bean>
<bean id=”timerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”simpleTriggerBean”/>
</list>
</property>
</bean>
</beans>
啟動程式的檔案相同。
3B:
如果需求中要求將來的某個時間點執行某項操作,那麼配置檔案中則必<wbr>須使用
org.springframework.scheduling<wbr>.quartz.CronTriggerBean。具體配置如下:
如果需求中要求將來的某個時間點執行某項操作,那麼配置檔案中則必
org.springframework.scheduling
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“[url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.JobDetailBean”>
<property name=”jobClass”>
<value>com.DemoJob</value>
</property>
<property name=”jobDataAsMap”>
<map>
<entry key=”jobData”>
<ref bean=”someData”/>
</entry>
</map>
</property>
</bean>
<bean id=”cronTriggerBean” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每天11點07分的時候程式執行一次–>
<property name=”cronExpression”>
<value>0 07 11 * * ?</value>
</property>
</bean>
<bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”cronTriggerBean”/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“[url]http://www.springframework.org[/url]/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.JobDetailBean”>
<property name=”jobClass”>
<value>com.DemoJob</value>
</property>
<property name=”jobDataAsMap”>
<map>
<entry key=”jobData”>
<ref bean=”someData”/>
</entry>
</map>
</property>
</bean>
<bean id=”cronTriggerBean” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每天11點07分的時候程式執行一次–>
<property name=”cronExpression”>
<value>0 07 11 * * ?</value>
</property>
</bean>
<bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”cronTriggerBean”/>
</list>
</property>
</bean>
</beans>
4:如果程式不允許繼承QuartzJobBean類<wbr>,我們怎樣處理呢?那麼我們可以採用spring提供的Metho<wbr>dInvokingJobDetailFactoryBean
來進行處理,
來進行處理,
package com;
/**
* 這裡不需要繼承quartz類了
* @author Administrator
*
*/
public class DemoJob {
private JobData jobData;
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
public void execute(){
System.out.println(jobData.getData() + ” 被執行了。”);
}
}
package com;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobData {
public String getData() {
SimpleDateFormat ddd = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
return “Data from “ + ddd.format(new Date());
}
}
/**
* 這裡不需要繼承quartz類了
* @author Administrator
*
*/
public class DemoJob {
private JobData jobData;
public JobData getJobData() {
return jobData;
}
public void setJobData(JobData jobData) {
this.jobData = jobData;
}
public void execute(){
System.out.println(jobData.getData() + ” 被執行了。”);
}
}
package com;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobData {
public String getData() {
SimpleDateFormat ddd = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
return “Data from “ + ddd.format(new Date());
}
}
具體的xml檔案配置如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”someJob” class=”com.DemoJob”>
<property name=”jobData”>
<ref bean=”someData”/>
</property>
</bean>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>
<property name=”targetObject”>
<ref bean=”someJob”/>
</property>
<property name=”targetMethod”>
<value>execute</value>
</property>
</bean>
<bean id=”cronTriggerBean” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每天11點07分的時候程式執行一次–>
<property name=”cronExpression”>
<value>0 27 11 * * ?</value>
</property>
</bean>
<bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”cronTriggerBean”/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<!–定義定時任務類–>
<bean id=”someData” class=”com.JobData”/>
<bean id=”someJob” class=”com.DemoJob”>
<property name=”jobData”>
<ref bean=”someData”/>
</property>
</bean>
<bean id=”jobDetailBean” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>
<property name=”targetObject”>
<ref bean=”someJob”/>
</property>
<property name=”targetMethod”>
<value>execute</value>
</property>
</bean>
<bean id=”cronTriggerBean” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
<!–這裡定義定時任務的物件的位置–>
<property name=”jobDetail”>
<ref bean=”jobDetailBean”/>
</property>
<!–這裡定義每天11點07分的時候程式執行一次–>
<property name=”cronExpression”>
<value>0 27 11 * * ?</value>
</property>
</bean>
<bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<property name=”triggers”>
<list>
<ref bean=”cronTriggerBean”/>
</list>
</property>
</bean>
</beans>