Spring定时器在SSH框架中的应用之---Java Timer调度器

在系统应用中,我们有很多的工作是需要系统自己来做的。在Spring中针对此需求有两种流行配置,一是Java的Timer类;二是Quartz调度器。

下面来介绍下第一种配置:Java的Timer类;

首先定义一个定时器任务,继承java.util.TimerTask类实现run方法:

import java.util.List;
import java.util.TimerTask;

import com.huilian.synchro.module.SynchroColumn;
import com.huilian.synchro.service.ISynchroColumnService;
import com.huilian.wcm.webservice.IContentWebserviceClient;
import com.huilian.wcm.webservice.IContentWebservicePortType;

public class SynchroDataTimerTask extends TimerTask{
    
    private IContentWebserviceClient client = null;
    IContentWebservicePortType service = null;
    
    public SynchroDataTimerTask(){
        client = new IContentWebserviceClient();
        service = client.getIContentWebserviceHttpPort();
    }
    private ISynchroColumnService synchroColumnService;
    public ISynchroColumnService getSynchroColumnService() {
        return synchroColumnService;
    }
    public void setSynchroColumnService(ISynchroColumnService synchroColumnService) {
        this.synchroColumnService = synchroColumnService;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stubList<SynchroColumn> synchroColumns = this.synchroColumnService.findAllColumns();
       for(int i=0;i<synchroColumns.size();i++){
            SynchroColumn synchroColumn = synchroColumns.get(i);
            System.out.println(synchroColumn.getNwcolumnid());
       }
    }
    
}
Run()方法定义了当任务运行时该做什么。synchroColumnService通过依赖注入的方式提供给SynchroDataTimerTask。

其次,在Spring配置文件中声明 SynchroDataTimerTask: 

<!-- 声明定时器任务 --> 
    <bean id="synchroDataTimerTask" class="com.huilian.synchro.task.SynchroDataTimerTask">
        <property name="synchroColumnService">
            <ref bean="synchroColumnService"/>
        </property>
    </bean>
    
    <!-- 调度定时器任务 --> 
    <bean id="scheduledDayDataTimerJob" class="org.springframework.scheduling.timer.ScheduledTimerTask">
        <property name="timerTask">
            <ref bean="synchroDataTimerTask" />
        </property>
        <property name="delay">
            <value>10000</value>
        </property>
        <property name="period">
            <value>3600000</value>
        </property>
    </bean> 
    
    <!-- 启动定时器 --> 
    <bean class="org.springframework.scheduling.timer.TimerFactoryBean"> 
        <property name="scheduledTimerTasks"> 
            <list> 
                <ref bean="scheduledDayDataTimerJob"/> 
            </list> 
        </property> 
    </bean> 

以上配置中属性timerTask告诉ScheduledTimerTask运行哪个TimerTask。再次,该属性装配了指向 scheduledDayDataTimerJob的一个引用,它就是synchroDataTimerTask。属性period告诉 ScheduledTimerTask以怎样的频度调用TimerTask的run()方法,该属性以毫秒作为单位。
属性delay允许你指定当任务第一次运行之前应该等待多久。在此指定DayDataTimerTask的第一次运行相 对于应用程序的启动时间延迟10秒钟。

Spring的TimerFactoryBean负责启动定时任务。属性scheduledTimerTasks要求一个需要启动的定时器任务的列表。在此只包含一个指向scheduledDayDataTimerJob的引用。 
   
Java Timer只能指定任务执行的频度,但无法精确指定它何时运行,这是它的一个局限性。要想精确指定任务的启动时间,就需要使用Quartz[kwɔ:ts]调度器。 

 

posted @ 2013-02-20 17:23  雨心竹  阅读(5930)  评论(0编辑  收藏  举报