使用spring配置定时器

来源:互联网 发布:北京海关数据分中心 编辑:程序博客网 时间:2024/06/05 19:54

首先创建一个定时器类:

@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文件

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

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

0 0
原创粉丝点击