欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 定时任务schedule

发布时间:2025/7/14 编程问答 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 springboot 定时任务schedule 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

测试版本

1.5 和 2.0.4 完全一样

 

两步

1. 配置类

2.job

 

1、配置类

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;@Configuration @EnableScheduling public class ScheduleConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler(taskExecutor());}@Bean(destroyMethod="shutdown")public ExecutorService taskExecutor() {return Executors.newScheduledThreadPool(5);} }

 

2. job

package com.ly.job;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;/*** 第一位,表示秒,取值0-59* 第二位,表示分,取值0-59* 第三位,表示小时,取值0-23* 第四位,日期天/日,取值1-31* 第五位,日期月份,取值1-12* 第六位,星期,取值1-7,1表示星期天,2表示星期一* 第七位,年份,可以留空,取值1970-2099*/ @Component public class HeartbeatJob {private static final Logger logger = LoggerFactory.getLogger(HeartbeatJob.class);/*** 检查状态1*/@Scheduled(cron = "0 0 10,16 * * ?")public void checkState1() {logger.info(">>>1.5版本>> cron下午16上传检查开始....");logger.info(">>>1.5版本>> cron下午16上传检查结束....");}/*** 检查状态2*/@Scheduled(cron = "0 0 18 * * ?")public void checkState2() {logger.info(">>>>> cron晚上18:00上传检查开始....");logger.info(">>>>> cron晚上18:00上传检查完成....");}/*** 检查状态3 5秒钟执行一次*///@Scheduled(cron = "0/5 * * * * ? ")@Scheduled(cron = "0/5 * * * * ?")public void checkState3() {logger.info(">>1.5版本>>>0/5 检查开始....");}}

 

转载于:https://www.cnblogs.com/lyon91/p/10103188.html

总结

以上是生活随笔为你收集整理的springboot 定时任务schedule的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。