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]调度器。 

 

转载于:https://www.cnblogs.com/yzy-lengzhu/archive/2013/02/20/2919095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值