Spring Boot 使用 @Scheduled 注解创建定时任务
在项目开发中我们经常需要一些定时任务来处理一些特殊的任务,比如定时检查订单的状态、定时同步数据等等。
在 Spring Boot 中使用 @Scheduled 注解创建定时任务非常简单,只需要两步操作就可以创建一个定时任务:
1、在定时任务类上增加
@EnableScheduling
注解
2、在要执行任务的方法上增加@Scheduled
注解
下面是我使用 cron 表达式创建一个简单的定时任务:
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
// 启用定时任务
@EnableScheduling
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
// 每 5 秒执行一次任务。
@Scheduled(cron = "0/5 * * * * ?")
public void performingTasks() {
log.info("执行定时任务时间:{}", dateFormat.format(new Date()));
}
}
启动运行项目以后控制台会每隔五秒打印一条记录。
2019-07-25 11:12:00.343 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:00
2019-07-25 11:12:05.343 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:05
2019-07-25 11:12:10.343 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:10
2019-07-25 11:12:15.343 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:15
2019-07-25 11:12:20.343 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:20
2019-07-25 11:12:25.342 INFO 15988 --- [pool-1-thread-1] c.m.s.s.a.controller.ScheduledTasks : 执行定时任务时间:11:12:25
一些常用的 Cron 表达式例子
表达式 | 说明 |
---|---|
0/2 * * * * ? | 表示每 2 秒执行任务 |
0 0/2 * * * ? | 表示每 2 分钟执行任务 |
0 0 2 1 * ? | 表示在每月的 1 日的凌晨 2 点调整任务 |
0 15 10 ? * MON-FRI | 表示周一到周五每天上午 10:15 执行作业 |
0 15 10 ? 6L 2002-2006 | 表示 2002-2006 年的每个月的最后一个星期五上午 10:15 执行作 |
0 0 10,14,16 * * ? | 每天上午 10 点,下午 2 点,4 点 |
0 0/30 9-17 * * ? | 朝九晚五工作时间内每半小时 |
0 0 12 ? * WED | 表示每个星期三中午 12 点 |
0 0 12 * * ? | 每天中午 12 点触发 |
0 15 10 ? * * | 每天上午 10:15 触发 |
0 15 10 * * ? | 每天上午 10:15 触发 |
0 15 10 * * ? | 每天上午 10:15 触发 |
0 15 10 * * ? 2005 | 2005 年的每天上午 10:15 触发 |
0 * 14 * * ? | 在每天下午 2 点到下午 2:59 期间的每 1 分钟触发 |
0 0/5 14 * * ? | 在每天下午 2 点到下午 2:55 期间的每 5 分钟触发 |
0 0/5 14,18 * * ? | 在每天下午 2 点到 2:55 期间和下午 6 点到 6:55 期间的每 5 分钟触发 |
0 0-5 14 * * ? | 在每天下午 2 点到下午 2:05 期间的每 1 分钟触发 |
0 10,44 14 ? 3 WED | 每年三月的星期三的下午 2:10 和 2:44 触发 |
0 15 10 ? * MON-FRI | 周一至周五的上午 10:15 触发 |
0 15 10 15 * ? | 每月 15 日上午 10:15 触发 |
0 15 10 L * ? | 每月最后一日的上午 10:15 触发 |
0 15 10 ? * 6L | 每月的最后一个星期五上午 10:15 触发 |
0 15 10 ? * 6L 2002-2005 | 2002 年至 2005 年的每月的最后一个星期五上午 10:15 触发 |
0 15 10 ? * 6#3 | 每月的第三个星期五上午 10:15 触发 |
@Scheduled
除了支持 cron 表达式以外还有很多的其他使用方法:
// 上一次开始执行时间点后 1 秒再次执行。
@Scheduled(fixedRate = 1000)
// 上一次执行完毕时间点后 1 秒再次执行。
@Scheduled(fixedDelay = 1000)
// 第一次延迟 2 秒执行,然后在上一次执行完毕时间点后 1 秒再次执行。
@Scheduled(initialDelay = 2000, fixedDelay = 1000)
网上也有一些 Cron 测试工具可以验证定时任务的执行时间:
1、https://tool.lu/crontab/
2、http://cron.qqe2.com/
Spring Boot 使用 @Scheduled 注解创建定时任务的更多相关文章
- 使用spring提供的@Scheduled注解创建定时任务
使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...
- Spring Boot中@Scheduled注解的使用方法
Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...
- Spring 的@Scheduled注解实现定时任务运行和调度
Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml --- 即spring的主配置文件(有的项目中叫做applicationContext.xm ...
- spring boot项目中处理Schedule定时任务
项目中,因为使用了第三方支付(支付宝和微信支付),支付完毕后,第三方支付平台一般会采用异步回调通知的方式,通知商户支付结果,然后商户根据通知内容,变更商户项目支付订单的状态.一般来说,为了防止商户项目 ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- Spring Boot常用的注解
@SpringBootApplication:包含@Configuration.@EnableAutoConfiguration.@ComponentScan通常用在主类上.由于大量项目都会在主要的配 ...
- java并发学习--第二章 spring boot实现线程的创建
除了之前介绍的创建线程方式外,spring boot为我们了提供一套完整的线程创建方式,其中包括了:线程.线程池.线程的监控. 一.使用spring boot提供的方法创建线程与线程池 1.首先在sp ...
- Spring Boot 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...
随机推荐
- scroll-view组件实现下拉刷新, 拉到底加载更多
官方文档已声明,即使在page.json和app.json中开启下拉刷新,scroll-view组件也是不支持的.但我们可以通过曲线救国的方法来实现 实现代码 // wxml <scroll-v ...
- How to transform the day time images to night time ? A series of paper review and some thinkings about this point.
How to transform the day time images to night time ? A series of paper review and some thinkings ab ...
- SEQ!org.apache.hadoop.io.LongWritable
[uhadoop@10-13-109-236 subdir26]$ $HADOOP_HOME/bin/hadoop fs -cat /data/flumeEvents/FlumeData.155980 ...
- iOS 点击空白处收回键盘的几个简单代码
//收回键盘1 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view.subviews enumer ...
- MySQL中创建存储过程示例
在这个示例中需要用到一张名为test_table的表,我们使用show create table test_table查看表的创建过程: CREATE TABLE `test_table` ( `id ...
- MySQL创建及删除临时表
示例SQL: drop temporary table if exists testdb.tmp_test_table; create temporary table testdb.tmp_test_ ...
- 利用PHP应用程序中的远程文件包含(RFI)并绕过远程URL包含限制
来源:http://www.mannulinux.org/2019/05/exploiting-rfi-in-php-bypass-remote-url-inclusion-restriction.h ...
- json 格式化输出
C#格式化JSON字符串 很多时候我们需要将json字符串以 { "status": 1, "sum": 9 }这种方式显示,而从服务端取回来的 ...
- 对图片清晰度问题,纠结了一晚上。清理了下Libray,瞬间变清晰了,泪奔
对图片清晰度问题,纠结了一晚上.清理了下Libray,瞬间变清晰了,泪奔
- Direct2D 学习笔记(3)图层 Layer
利用图层Layer绘制资源网址:https://docs.microsoft.com/zh-cn/windows/win32/direct2d/direct2d-layers-overview 1 ...