Spring 定时器可以使用注解,免去了繁琐的配置.
@Component
@Configurable
@EnableScheduling
public class ScTask{
@Scheduled(fixedRate = 1000 * 3)
public void reportCurrentTime(){
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));
}
//每1分钟执行一次
@Scheduled(cron = "0 */1 * * * * ")
public void reportCurrentByCron(){
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}
private SimpleDateFormat dateFormat(){
return new SimpleDateFormat("HH:mm:ss");
}
}
@EnableScheduling:标注启动定时任务
@Scheduled:标注启动定时任务
@Scheduled(fixdRate = 1000*3)定时执行