Spring中使用任务计划(转)
文件了,因为我们使用的是spring来管理任务计划。
继承TimerTask类的写法如下:
import java.util.TimerTask;
public class DemoTask extends TimerTask {
public void run() {
System.out.println("任务程序启动,这时我的第一个任务程序......");
}
}
<!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>
启动程序的写法:
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);
}
}
}
}
xml的配置文件的写法,程序中需要指定执行哪一个类的哪一个方法:
<!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>
推荐使用Quartz来解决这样的问题。接下来的这个程序仍然是解决周期问题的,要循序渐进。。。
QuartzJobBean 类用于重复调用JobData 类的方法执行getData()方法。QuartzJobBean 必须继承QuartzJobBean
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());
}
}
<!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>
启动程序的文件相同。
如果需求中要求将来的某个时间点执行某项操作,那么配置文件中则必须使用
org.springframework.scheduling.quartz.CronTriggerBean。具体配置如下:
<!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>
来进行处理,
/**
* 这里不需要继承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());
}
}
<!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>
本文出自 “自娱自乐” 博客,请务必保留此出处http://sunny.blog.51cto.com/182601/32366
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。