springboot 定时任务
启用定时任务配置
@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());
}
}
本文来自博客园,作者:独行侠X,转载请注明原文链接:https://www.cnblogs.com/geekswg/p/15958754.html