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());
    }
}

作者:geekswg

出处:https://www.cnblogs.com/geekswg/p/15958754.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   独行侠X  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题