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 閱讀(4294) 評論(0)  編輯  收藏

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 昌吉市| 太原市| 徐汇区| 离岛区| 岱山县| 墨脱县| 柳江县| 花莲县| 区。| 阿尔山市| 新安县| 班玛县| 彩票| 交城县| 库尔勒市| 衡水市| 京山县| 东源县| 安阳县| 马鞍山市| 庆元县| 萝北县| 巩留县| 古丈县| 淮滨县| 江源县| 托克逊县| 舞钢市| 德令哈市| 泰和县| 尼勒克县| 南靖县| 云南省| 高碑店市| 攀枝花市| 闽侯县| 雷山县| 桃园市| 呼和浩特市| 开远市| 山丹县|