美文网首页SpringBoot
Scheduling定时器

Scheduling定时器

作者: WebGiser | 来源:发表于2023-01-09 14:19 被阅读0次
启动类上添加 @EnableScheduling
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledTaskService {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void fixedRateTask() {
        System.out.println("fixedRateTask 每5s执行一次: " + dateFormat.format(new Date()));
    }

    @Scheduled(cron = "0 30 14 ? * *")
    public void cronTask() {
        System.out.println("cronTask 每天14点30分执行一次: " + dateFormat.format(new Date()));
    }
}

相关文章

网友评论

    本文标题:Scheduling定时器

    本文链接:https://www.haomeiwen.com/subject/kuopcdtx.html