使用spring配置定时器

首先创建一个定时器类:

@Component("cacheTimer")
public class CacheTimer extends TimerTask {
    public static boolean isRun = false;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        if(!isRun){
            isRun = true;
            System.out.println("this is run!");
            isRun = false;
        }
    }
}

然后配置applicationContext.xml文件

id="cacheExecutor" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
    <property name="runnable" ref="cacheTimer" />
    <property name="delay" value="0" />
    <property name="period" value="3600000" />


class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
    <property name="poolSize" value="1" />
    <property name="scheduledExecutorTasks">
        <list>
            <ref bean="cacheExecutor"/>
        list>
    property>

这样就完成了一个定时器的配置

你可能感兴趣的:(spring,spring,定时器)