您现在的位置是:主页 > news > 珠海市横琴新区建设环保局网站/网络营销的三种方式
珠海市横琴新区建设环保局网站/网络营销的三种方式
admin2025/5/5 13:40:20【news】
简介珠海市横琴新区建设环保局网站,网络营销的三种方式,重庆网站建设求职简历,西安专业网站制作SpringBoot创建定时任务1. 首先在启动类加 EnableScheduling 注解,开启定时任务2. 然后在类中,导入Schedule包并添加注解3. Scheduled 常用时间1. 首先在启动类加 EnableScheduling 注解,开启定时任务 import org.springframework.scheduling…
珠海市横琴新区建设环保局网站,网络营销的三种方式,重庆网站建设求职简历,西安专业网站制作SpringBoot创建定时任务1. 首先在启动类加 EnableScheduling 注解,开启定时任务2. 然后在类中,导入Schedule包并添加注解3. Scheduled 常用时间1. 首先在启动类加 EnableScheduling 注解,开启定时任务 import org.springframework.scheduling…
SpringBoot创建定时任务
- 1. 首先在启动类加 @EnableScheduling 注解,开启定时任务
- 2. 然后在类中,导入Schedule包并添加注解
- 3. @Scheduled 常用时间
1. 首先在启动类加 @EnableScheduling 注解,开启定时任务
import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling // 开启定时任务
2. 然后在类中,导入Schedule包并添加注解
- 导包
import org.springframework.scheduling.annotation.Scheduled;
- 方法上加注解
//服务实现类
@Service
@Slf4j
public class 类名 extends ServiceImpl<泛型> implements XXXService {//定时任务,每15分钟执行一次@Scheduled(cron = "0 0/15 * * * ?")public void timingTask(){//设置时间格式,用来转换字符串DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//开始时间Date beginTime = new Date();//利用时间格式,把开始时间转为字符串String start = df.format(beginTime);log.info("定时任务开始,开始时间为:"+ start);//---------//这里可以写 需要执行的代码,时间到了会自动执行//----------//获取结束时间Date finishTime = new Date();log.info("定时任务结束,结束时间为:"+ df.format(finishTime));//开始时间 转为 长整型 LongLong begin = beginTime.getTime();//结束时间 转为 Long 类型Long end = finishTime.getTime();// 时间差 = 结束时间 - 开始时间,这样得到的差值是毫秒级别long timeLag = end - begin;//java计算时间差的代码,在我的另一篇文章里。}
}
3. @Scheduled 常用时间
- 每小时
//每小时0分0秒执行一次
@Scheduled(cron = "0 0 * * * ?")
- 每两小时
//每两个小时执行一次
@Scheduled(cron = "0 0 */2 * * ?")
- 每天凌晨两点
每天凌晨两点执行
@Scheduled(cron = "0 0 2 * * ?")
一个cron表达式,至少6个(有可能7个)用空格分隔的时间元素
位置 | 第一个 * | 第二个 * | 第三个 * | 第四个 * | 第五个 * | 第六个 * | 第七个 * |
---|---|---|---|---|---|---|---|
含义 | 秒 | 分 | 时 | 日 | 月 | 周 | 年 |
范围 | 0~59 | 0~59 | 0~23 | 1~31 | 1~12 | 1~7 | 1970~2099 |
是否必填 | 是 | 是 | 是 | 是 | 是 | 是 | 否 |
required | true | true | true | true | true | true | false |
- 其中 周 是 星期几 的意思,1~7 也可以写成 SUN,MON,TUE,WED,THU,FRI,SAT
- 推荐一篇csdn文章: springboot整合定时任务(自动,手动).
2022年2月这篇文章已被收藏 70,个人认为写的还是蛮详细的。