启用定时任务配置
@SpringBootApplication
@EnableScheduling
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
创建定时任务类
@Component
public class TimerTask {
private static final long TIME_DELAY = 2 * 1000; // 60 秒
//每 隔 TIME_DELAY 执行一次
@Scheduled(fixedDelay =TIME_DELAY)
public void delayTask(){
System.out.println("fixedDelay Task => " + DateUtil.now());
}
//每五秒执行一次
@Scheduled(cron="0/5 * * * * ?")
public void cronTask(){
System.out.println("corn Task => "+DateUtil.now());
}
}