fun

 

Spring中的定时任务介绍

下面我们来看一下Spring中提供的定时任务开发:
在Spring中开发定时任务,分为3个步骤。
1 创建定时任务
2 注册定时任务
3 启动定时任务
分别来看一下
1 创建定时任务:

package org.jnotnull;
import java.util.TimerTask;
public class MyTesk extends TimerTask{
....
public void run(){
//添加任务
}
....
}

2 注册定时任务,并设置参数
我们来配置TimerConfig.xml防御WEB-INF下

<bean id="myTesk" class="edu.cumt.jnotnull.action.TaskAction">  
        
<property name="newsManageService">  
            
<ref bean="newsManageService" />  
        
</property>  
    
</bean>  
    
<bean id="stTask"  
        class
="org.springframework.scheduling.timer.ScheduledTimerTask">  
        
<property name="delay">  
            
<value>20000</value>  
        
</property>  
        
<property name="period">  
            
<value>30000</value>  
        
</property>  
        
<property name="timerTask">  
            
<ref bean="myTesk" />  
        
</property>  
    
</bean>  
    
<bean id="timerFactory"  
        class
="org.springframework.scheduling.timer.TimerFactoryBean">  
        
<property name="scheduledTimerTasks">  
            
<list>  
                
<ref bean="stTask" />  
            
</list>  
        
</property>  
    
</bean>  
3 启动定时任务   
<PRE class=xml name="code"><?xml version="1.0" encoding="UTF-8"?>  
<web-app>  
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>http://www.bt285.cn /WEB-INF/TimerConfig.xml</param-value>  
</context-param>  
<listener>  
<listener-class>  
 org.springframework.web.context.ContextLoaderListener   
</listener-class>  
</listener>  
</web-app>  
</PRE>  
<BR>  
<BR>下面我们再来看看在Spring中如何使用Quartz实现定时功能   
<BR>1 创建定时任务:   
<BR><PRE class=java name="code">package org.jnotnull;   
import java.util.TimerTask;   
/**
*http://www.5a520.cn
*/
public class MyTesk extends TimerTask{   
 
public void excute(){   
//添加任务   
}   
.   
}   
</PRE>  
<BR>2 注册定时任务,并设置参数   
<BR>我们来配置TimerConfig.xml防御WEB-INF下   
<BR><PRE class=xml name="code"><?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 ="myTesk" class="org.jnotnull.MyTesk"/>  
<bean id ="myJob"    
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
<property name="targetObject">  
<ref bean="myTask">  
</property>  
<propertyproperty ="targetMethod">  
<value>execute</value>  
</property>  
</bean>  
<bean id ="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail">  
 <ref bean="myJob">  
</property>  
<property name="cronExpression">  
<value>12,23****?</value>  
</property>  
</bean>  
<bean id ="timerFactory"    
class="org.springframework.scheduling.quartz.ScheduleFactoryBean">  
<property name="triggers">  
<list>  
<refref="timeTrigger">  
</list>  
</property>  
</bean>  
</beans>  
</PRE>  
<BR>3 启动定时任务   
<BR><PRE class=xml name="code"><?xml version="1.0" encoding="UTF-8"
?>  
<web-app>  
<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value> http://www.bt285.cn /WEB-INF/TimerConfig.xml</param-value>  
</context-param>  
<listener>  
<listener-class>  
 org.springframework.web.context.ContextLoaderListener   
</listener-class>  
</listener>  
</web-app>  
</PRE>    

posted on 2009-05-07 18:59 fun 阅读(4276) 评论(0)  编辑  收藏

导航

统计

常用链接

留言簿(11)

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜