Spring之ScheduledExecutorTask任务调度

 实现步骤:

1、创建一个类实现runable

package com.cj.eduresource.tools;
                           
import com.cj.eduresource.service.EduResourceService;

public class ScanFolderTimeTask implements Runnable {
                           
    private EduResourceService eduResourceService;
    public void setEduResourceService(EduResourceService eduResourceService) {
        this.eduResourceService = eduResourceService;
    }
                           
    @Override
    public void run() {
        String path="D:\\Test";
        try {
            if(null != eduResourceService)                      this.eduResourceService.createEduResource(path);
                                       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
                           
}

 

2、配置Spring

<bean id="eduResourceScanFolderTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
      <!-- 定义需要执行任务的类 -->
        <property name="runnable">
            <bean class="com.cj.eduresource.tools.ScanFolderTimeTask">
                <property name="eduResourceService" ref="eduResourceService" />
            </bean>
        </property>
        <!-- 系统启动后,延迟50秒启动该任务 -->
        <property name="delay" value="50000"/>
        <!-- 每隔1小时重复执行一次该任务 -->
        <property name="period" value="3600000"/>
 </bean>

 

3、配置任务调度

<bean id="scheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
        <property name="poolSize" value="5" />
        <property name="scheduledExecutorTasks">
            <list>
                <ref bean="eduResourceScanFolderTask"/>
                <!-- 如有多任务,可以如1、2部配置bean,再将任务bean放到list中 -->
            </list>
        </property>
</bean>    

 

posted @ 2014-04-02 13:57  金牛十三兵  阅读(2788)  评论(0编辑  收藏  举报