Spring中使用任务计划

yupi0 2010-05-28

本文转自:http://sunny.blog.51cto.com/182601/32366

关于在Spring中的任务计划的使用

我今天结合Spring技术手册中的内容,总共总结了5个方面:

1:使用最简单的任务计划,就是继承java.util.TimerTask类,最关键的当然是配置beans-config.xml

文件了,因为我们使用的是spring来管理任务计划。

继承TimerTask类的写法如下:

packagecom;

importjava.util.TimerTask;

publicclassDemoTaskextendsTimerTask{

publicvoidrun(){

System.out.println("任务程序启动,这时我的第一个任务程序......");

}

}

beans-config.xml的写法如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--定义定时任务类-->

<beanid="demoTask"class="com.DemoTask"/>

<beanid="scheduledTimerTask"class="org.springframework.scheduling.timer.ScheduledTimerTask">

<!--这里定义定时任务的对象的位置-->

<propertyname="timerTask">

<refbean="demoTask"/>

</property>

<!--这里定义每六秒钟程序执行一次-->

<propertyname="period">

<value>6000</value>

</property>

<!--这里定义程序启动两秒钟后开始执行-->

<propertyname="delay">

<value>2000</value>

</property>

</bean>

<beanid="timerFactoryBean"class="org.springframework.scheduling.timer.TimerFactoryBean">

<propertyname="scheduledTimerTasks">

<list>

<refbean="scheduledTimerTask"/>

</list>

</property>

</bean>

</beans>

启动程序的写法:

packagecom;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importorg.springframework.context.support.FileSystemXmlApplicationContext;

publicclassTimerTaskDemo{

/**

*@authorsunny这个程序用于测试spring框架中的任务管理,这个一个最简单地实现定时任务的方式

*@paramargs

*/

publicstaticvoidmain(String[]args)throwsException{

newFileSystemXmlApplicationContext("beans-config.xml");

System.out.println("启动任务...");

System.out.println("请输入exit关闭程序.....");

BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in));

while(true){

if(reader.readLine().equals("exit")){

System.exit(0);

}

}

}

}

2:当我们在程序中不能继承TimerTask类了,我们应该怎样写呢?那我们就需要在程序中使用spring提供的"org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"类来进行处理了。不同的地方就是

xml的配置文件的写法,程序中需要指定执行哪一个类的哪一个方法:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--定义定时任务类-->

<beanid="demoTask"class="com.DemoTask"/>

<beanid="timerTaskBean"class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">

<propertyname="targetObject">

<refbean="demoTask"/>

</property>

<propertyname="targetMethod">

<value>run</value>

</property>

</bean>

<beanid="scheduledTimerTask"class="org.springframework.scheduling.timer.ScheduledTimerTask">

<!--这里定义定时任务的对象的位置-->

<propertyname="timerTask">

<refbean="timerTaskBean"/>

</property>

<!--这里定义每六秒钟程序执行一次-->

<propertyname="period">

<value>6000</value>

</property>

<!--这里定义程序启动两秒钟后开始执行-->

<propertyname="delay">

<value>2000</value>

</property>

</bean>

<beanid="timerFactoryBean"class="org.springframework.scheduling.timer.TimerFactoryBean">

<propertyname="scheduledTimerTasks">

<list>

<refbean="scheduledTimerTask"/>

</list>

</property>

</bean>

</beans>

3A:由于jdk标准api提供的Timer功能有限,只能指定任务与任务之间的周期,而无法指定某个特定的时间内执行某项任务,所以现在

推荐使用Quartz来解决这样的问题。接下来的这个程序仍然是解决周期问题的,要循序渐进。。。

QuartzJobBean类用于重复调用JobData类的方法执行getData()方法。QuartzJobBean必须继承QuartzJobBean

packagecom;

importorg.quartz.JobExecutionContext;

importorg.springframework.scheduling.quartz.QuartzJobBean;

publicclassDemoJobextendsQuartzJobBean{

privateJobDatajobData;

protectedvoidexecuteInternal(JobExecutionContextarg0){

System.out.println(jobData.getData()+"第一个已经被执行了!!");

}

publicJobDatagetJobData(){

returnjobData;

}

publicvoidsetJobData(JobDatajobData){

this.jobData=jobData;

}

}

packagecom;

importjava.text.SimpleDateFormat;

importjava.util.Date;

publicclassJobData{

publicStringgetData(){

SimpleDateFormatddd=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");

return"Datafrom"+ddd.format(newDate());

}

}

关键在于beans-config.xml配置文件的写法

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--定义定时任务类-->

<beanid="someData"class="com.JobData"/>

<beanid="jobDetailBean"class="org.springframework.scheduling.quartz.JobDetailBean">

<propertyname="jobClass">

<value>com.DemoJob</value>

</property>

<propertyname="jobDataAsMap">

<map>

<entrykey="jobData">

<refbean="someData"/>

</entry>

</map>

</property>

</bean>

<beanid="simpleTriggerBean"class="org.springframework.scheduling.quartz.SimpleTriggerBean">

<!--这里定义定时任务的对象的位置-->

<propertyname="jobDetail">

<refbean="jobDetailBean"/>

</property>

<!--这里定义每六秒钟程序执行一次-->

<propertyname="repeatInterval">

<value>6000</value>

</property>

<!--这里定义程序启动两秒钟后开始执行-->

<propertyname="startDelay">

<value>2000</value>

</property>

</bean>

<beanid="timerFactoryBean"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<propertyname="triggers">

<list>

<refbean="simpleTriggerBean"/>

</list>

</property>

</bean>

</beans>

启动程序的文件相同。

3B:

如果需求中要求将来的某个时间点执行某项操作,那么配置文件中则必须使用

org.springframework.scheduling.quartz.CronTriggerBean。具体配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--定义定时任务类-->

<beanid="someData"class="com.JobData"/>

<beanid="jobDetailBean"class="org.springframework.scheduling.quartz.JobDetailBean">

<propertyname="jobClass">

<value>com.DemoJob</value>

</property>

<propertyname="jobDataAsMap">

<map>

<entrykey="jobData">

<refbean="someData"/>

</entry>

</map>

</property>

</bean>

<beanid="cronTriggerBean"class="org.springframework.scheduling.quartz.CronTriggerBean">

<!--这里定义定时任务的对象的位置-->

<propertyname="jobDetail">

<refbean="jobDetailBean"/>

</property>

<!--这里定义每天11点07分的时候程序执行一次-->

<propertyname="cronExpression">

<value>00711**?</value>

</property>

</bean>

<beanid="schedulerFactoryBean"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<propertyname="triggers">

<list>

<refbean="cronTriggerBean"/>

</list>

</property>

</bean>

</beans>

4:如果程序不允许继承QuartzJobBean类,我们怎样处理呢?那么我们可以采用spring提供的MethodInvokingJobDetailFactoryBean

来进行处理,

packagecom;

/**

*这里不需要继承quartz类了

*@authorAdministrator

*

*/

publicclassDemoJob{

privateJobDatajobData;

publicJobDatagetJobData(){

returnjobData;

}

publicvoidsetJobData(JobDatajobData){

this.jobData=jobData;

}

publicvoidexecute(){

System.out.println(jobData.getData()+"被执行了。");

}

}

packagecom;

importjava.text.SimpleDateFormat;

importjava.util.Date;

publicclassJobData{

publicStringgetData(){

SimpleDateFormatddd=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");

return"Datafrom"+ddd.format(newDate());

}

}

具体的xml文件配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--定义定时任务类-->

<beanid="someData"class="com.JobData"/>

<beanid="someJob"class="com.DemoJob">

<propertyname="jobData">

<refbean="someData"/>

</property>

</bean>

<beanid="jobDetailBean"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<propertyname="targetObject">

<refbean="someJob"/>

</property>

<propertyname="targetMethod">

<value>execute</value>

</property>

</bean>

<beanid="cronTriggerBean"class="org.springframework.scheduling.quartz.CronTriggerBean">

<!--这里定义定时任务的对象的位置-->

<propertyname="jobDetail">

<refbean="jobDetailBean"/>

</property>

<!--这里定义每天11点07分的时候程序执行一次-->

<propertyname="cronExpression">

<value>02711**?</value>

</property>

</bean>

<beanid="schedulerFactoryBean"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<propertyname="triggers">

<list>

<refbean="cronTriggerBean"/>

</list>

</property>

</bean>

</beans>

相关推荐