关于在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类了,我们应该怎样写呢?那我们就需要在程序中使用spring提供的"org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"类来进行处理了。不同的地方就是
xml的配置文件的写法,程序中需要指定执行哪一个类的哪一个方法:
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功能有限,只能指定任务与任务之间的周期,而无法指定某个特定的时间内执行某项任务,所以现在
推荐使用Quartz来解决这样的问题。接下来的这个程序仍然是解决周期问题的,要循序渐进。。。
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:
如果需求中要求将来的某个时间点执行某项操作,那么配置文件中则必须使用
org.springframework.scheduling.quartz.CronTriggerBean。具体配置如下:
如果需求中要求将来的某个时间点执行某项操作,那么配置文件中则必须使用
org.springframework.scheduling.quartz.CronTriggerBean。具体配置如下:
<?
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类,我们怎样处理呢?那么我们可以采用spring提供的MethodInvokingJobDetailFactoryBean
来进行处理,
具体的xml文件配置如下:
来进行处理,
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
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 >
转载于:https://blog.51cto.com/sunny/32366