springboot使用@Scheduled实现定时任务
1.代码实现:
启动类添加注解
@SpringBootApplication @EnableScheduling public class ScheduleApplication { public static void main(String[] args) { SpringApplication.run(ScheduleApplication.class, args); } }
添加定时任务类
@Component @Slf4j public class ScheduledTasks { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { log.info("现在时间:" + dateFormat.format(new Date())); } }
@Scheduled的参数:
- cron:通过cron表达式来配置执行规则
- zone:cron表达式解析时使用的时区
- fixedDelay:上一次执行结束到下一次执行开始的间隔时间(单位:ms)
- fixedDelayString:上一次任务执行结束到下一次执行开始的间隔时间,使用java.time.Duration#parse解析
- fixedRate:以固定间隔执行任务,即上一次任务执行开始到下一次执行开始的间隔时间(单位:ms),若在调度任务执行时,上一次任务还未执行完毕,会加入worker队列,等待上一次执行完成后立即执行下一次任务
- fixedRateString:与fixedRate逻辑一致,只是使用java.time.Duration#parse解析
- initialDelay:首次任务执行的延迟时间
- initialDelayString:首次任务执行的延迟时间,使用java.time.Duration#parse解析
2.实现效果:
运行项目,查看控制台
2021-12-28 16:55:10.684 INFO 15924 --- [ scheduling-1] com.cxh.schedule.task.ScheduledTasks : 现在时间:16:55:10 2021-12-28 16:55:15.681 INFO 15924 --- [ scheduling-1] com.cxh.schedule.task.ScheduledTasks : 现在时间:16:55:15 2021-12-28 16:55:20.683 INFO 15924 --- [ scheduling-1] com.cxh.schedule.task.ScheduledTa