Loading

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());
    }
}
posted @ 2022-03-03 10:54  独行侠X  阅读(42)  评论(0编辑  收藏  举报