Task:
public class SayHelloTask extends TimerTask
... {
@Override
public void run()
...{
System.out.println(" timer ");
}
}
public class SayHelloTask extends TimerTask
... {
@Override
public void run()
...{
System.out.println(" timer ");
}
}
程序调用:
public static void main(String[] args)
... {
Timer timer=new Timer();
timer.scheduleAtFixedRate(new SayHelloTask(),0, 3000);
}
public static void main(String[] args)
... {
Timer timer=new Timer();
timer.scheduleAtFixedRate(new SayHelloTask(),0, 3000);
}
spring 配置:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
< beans >
< bean name ="sayHelloTask" class ="com.timer.SayHelloTask" ></ bean >
< bean id ="scheduledTask" class ="org.springframework.scheduling.timer.ScheduledTimerTask" >
< property name ="timerTask" >
< ref bean ="sayHelloTask" />
</ property >
<!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档 -->
< property name ="period" >
< value > 5000 </ value >
</ property >
<!-- 延时1m 执行任务 -->
< property name ="delay" >
< value > 1000 </ value >
</ property >
</ bean >
<!-- 启动定时器 -->
< bean id ="timerBean" class ="org.springframework.scheduling.timer.TimerFactoryBean" >
< property name ="scheduledTimerTasks" >
< list >
< ref bean ="scheduledTask" />
</ list >
</ property >
</ bean >
</ beans >