定时任务实现的几种方式:
Timer: 这是 java 自带的 java.util.Timer 类, 这个类允许你调度一个 java.util.TimerTask 任务. 使用这种方式可以让你的程序按照某一个频度执行, 但不能在指定时间运行. 一般用的较少.
ScheduledExecutorService: 也 jdk 自带的一个类; 是基于线程池设计的定时任务类, 每个调度任务都会分配到线程池中的一个线程去执行, 也就是说, 任务是并发执行, 互不影响.
Spring Task:Spring3.0 以后自带的 task, 可以将它看成一个轻量级的 Quartz, 而且使用起来比 Quartz 简单许多.
Quartz: 这是一个功能比较强大的的调度器, 可以让你的程序在指定时间执行, 也可以按照某一个频度执行, 配置起来稍显复杂.
使用 Timer
这个目前在项目中用的较少, 直接贴 demo 代码. 具体的介绍可以查看 API
- public class TestTimer {
- public static void main(String[] args) {
- TimerTask timerTask = new TimerTask() {
- @Override
- public void run() {
- System.out.println("task run:"+ new Date());
- }
- };
- Timer timer = new Timer();
- // 安排指定的任务在指定的时间开始进行重复的固定延迟执行. 这里是每 3 秒执行一次
- timer.schedule(timerTask,10,3000);
- }
- }12345678910111213
使用 ScheduledExecutorService
该方法跟 Timer 类似, 直接看 demo:
- public class TestScheduledExecutorService {
- public static void main(String[] args) {
- ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
- // 参数: 1, 任务体 2, 首次执行的延时时间
- // 3, 任务执行间隔 4, 间隔时间单位
- service.scheduleAtFixedRate(()->System.out.println("task ScheduledExecutorService"+new Date()), 0, 3, TimeUnit.SECONDS);
- }
- }12345678
使用 Spring Task
简单的定时任务
在 SpringBoot 项目中, 我们可以很优雅的使用注解来实现定时任务, 首先创建项目, 导入依赖:
- <dependencies>
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-web
- </artifactId>
- </dependency>
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter
- </artifactId>
- </dependency>
- <dependency>
- <groupId>
- org.projectlombok
- </groupId>
- <artifactId>
- lombok
- </artifactId>
- <optional>
- true
- </optional>
- </dependency>
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-test
- </artifactId>
- <scope>
- test
- </scope>
- </dependency>
- </dependencies>
- 1234567891011121314151617181920
创建任务类:
- @Slf4j
- @Component
- public class ScheduledService {
- @Scheduled(cron = "0/5 * * * * *")
- public void scheduled(){
- log.info("=====>>>>>使用 cron {}",System.currentTimeMillis());
- }
- @Scheduled(fixedRate = 5000)
- public void scheduled1() {
- log.info("=====>>>>>使用 fixedRate{}", System.currentTimeMillis());
- }
- @Scheduled(fixedDelay = 5000)
- public void scheduled2() {
- log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis());
- }
- }12345678910111213141516
在主类上使用 @EnableScheduling 注解开启对定时任务的支持, 然后启动项目
可以看到三个定时任务都已经执行, 并且使同一个线程中串行执行, 如果只有一个定时任务, 这样做肯定没问题, 当定时任务增多, 如果一个任务卡死, 会导致其他任务也无法执行.
多线程执行
在传统的 Spring 项目中, 我们可以在 xml 配置文件添加 task 的配置, 而在 SpringBoot 项目中一般使用 config 配置类的方式添加配置, 所以新建一个 AsyncConfig 类
- @Configuration
- @EnableAsync
- public class AsyncConfig {
- /*
- 此处成员变量应该使用 @Value 从配置中读取
- */
- private int corePoolSize = 10;
- private int maxPoolSize = 200;
- private int queueCapacity = 10;
- @Bean
- public Executor taskExecutor() {
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
- executor.setCorePoolSize(corePoolSize);
- executor.setMaxPoolSize(maxPoolSize);
- executor.setQueueCapacity(queueCapacity);
- executor.initialize();
- return executor;
- }
- }12345678910111213141516171819
@Configuration: 表明该类是一个配置类
@EnableAsync: 开启异步事件的支持
然后在定时任务的类或者方法上添加 @Async . 最后重启项目, 每一个任务都是在不同的线程中
执行时间的配置
在上面的定时任务中, 我们在方法上使用 @Scheduled 注解来设置任务的执行时间, 并且使用三种属性配置方式:
fixedRate: 定义一个按一定频率执行的定时任务
fixedDelay: 定义一个按一定频率执行的定时任务, 与上面不同的是, 改属性可以配合 initialDelay, 定义该任务延迟执行时间.
cron: 通过表达式来配置任务执行时间
cron 表达式详解
一个 cron 表达式有至少 6 个 (也可能 7 个) 有空格分隔的时间元素. 按顺序依次为:
秒(0~59)
分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
其中每个元素可以是一个值(如 6), 一个连续区间(9-12), 一个间隔时间(8-18/4)(/ 表示每隔 4 小时), 一个列表(1,3,5), 通配符. 由于 "月份中的日期" 和 "星期中的日期" 这两个元素互斥的, 必须要对其中一个设置. 配置实例:
每隔 5 秒执行一次:/5 * * * ?
每隔 1 分钟执行一次: 0 /1 * * ?
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 触发
有些子表达式能包含一些范围或列表
例如: 子表达式 (天(星期)) 可以为 "MON-FRI","MON,WED,FRI","MON-WED,SAT"
"*" 字符代表所有可能的值
"/" 字符用来指定数值的增量
例如: 在子表达式 (分钟) 里的 "0/15" 表示从第 0 分钟开始, 每 15 分钟
在子表达式 (分钟) 里的 "3/20" 表示从第 3 分钟开始, 每 20 分钟 (它和 "3,23,43") 的含义一样
"?" 字符仅被用于天 (月) 和天 (星期) 两个子表达式, 表示不指定值
当 2 个子表达式其中之一被指定了值以后, 为了避免冲突, 需要将另一个子表达式的值设为 "?"
"L" 字符仅被用于天 (月) 和天 (星期) 两个子表达式, 它是单词 "last" 的缩写
如果在 "L" 前有具体的内容, 它就具有其他的含义了.
例如:"6L" 表示这个月的倒数第6天
注意: 在使用 "L" 参数时, 不要指定列表或范围, 因为这会导致问题
W 字符代表着平日(Mon-Fri), 并且仅能用于日域中. 它用来指定离指定日的最近的一个平日. 大部分的商业处理都是基于工作周的, 所以 W 字符可能是非常重要的.
例如, 日域中的 15W 意味着 "离该月 15 号的最近一个平日." 假如 15 号是星期六, 那么 trigger 会在 14 号 (星期五) 触发, 因为星期四比星期一离 15 号更近.
C: 代表 "Calendar" 的意思. 它的意思是计划所关联的日期, 如果日期没有被关联, 则相当于日历中所有日期.
例如 5C 在日期字段中就相当于日历 5 日以后的第一天. 1C 在星期字段中相当于星期日后的第一天.
字段
允许值
允许的特殊字符
秒
0~59
, - */
分
0~59
, - */
小时
0~23
, - */
日期
- 1-31
- , - * ? / L W C
月份
1~12 或者 JAN~DEC
, - */
星期
1~7 或者 SUN~SAT
, - * ? / L C #
年(可选)
留空, 1970~2099
, - */
在线 cron 表达式生成: http://qqe2.com/cron/index
整合 Quartz
添加依赖
如果 SpringBoot 版本是 2.0.0 以后的, 则在 spring-boot-starter 中已经包含了 quart 的依赖, 则可以直接使用 spring-boot-starter-quartz 依赖:
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-quartz
- </artifactId>
- </dependency>
- 1234
如果是 1.5.9 则要使用以下添加依赖:
- <dependency>
- <groupId>
- org.quartz-scheduler
- </groupId>
- <artifactId>
- quartz
- </artifactId>
- <version>
- 2.3.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-context-support
- </artifactId>
- </dependency>
- 123456789
这里我使用 SpringBoot 版本是 2.0.0.BUILD-SNAPSHOT , 该版本开始集成了 Quartz, 所以事实现起来很方便. 其它好像比较麻烦, 这里就不介绍, 以后有时间再详细深入了解 Quartz.
创建任务类 TestQuartz, 该类主要是继承了 QuartzJobBean
- public class TestQuartz extends QuartzJobBean {
- /**
- * 执行定时任务
- * @param jobExecutionContext
- * @throws JobExecutionException
- */
- @Override
- protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
- System.out.println("quartz task"+new Date());
- }
- }1234567891011
创建配置类 QuartzConfig
- @Configuration
- public class QuartzConfig {
- @Bean
- public JobDetail teatQuartzDetail(){
- return JobBuilder.newJob(TestQuartz.class).withIdentity("testQuartz").storeDurably().build();
- }
- @Bean
- public Trigger testQuartzTrigger(){
- SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
- .withIntervalInSeconds(10) // 设置时间周期单位秒
- .repeatForever();
- return TriggerBuilder.newTrigger().forJob(teatQuartzDetail())
- .withIdentity("testQuartz")
- .withSchedule(scheduleBuilder)
- .build();
- }
- }123456789101112131415161718
启动项目
最后
上面都是简单的介绍了关于 SpringBoot 定时任务的处理, 直接使用 SpringTask 注解的方式应该是最方便的, 而使用 Quartz 从 2.0 开始也变得很方便. 对于这两种方式, 应该说各有长处吧, 按需选择. 另外关于 Quartz 的详细内容可以查看官方文档: 传送门
参考
spring 定时任务 @Scheduled
springboot 中使用定时任务, 异步调用, 自定义配置参数(八)
来源: http://www.bubuko.com/infodetail-2901131.html