> 文章列表 > 分布式定时任务

分布式定时任务

分布式定时任务

本文引用了谷粒商城的课程

定时任务

定时任务是我们系统里面经常要用到的一些功能。如每天的支付订单要与支付宝进行对账操作、每个月定期进行财务汇总、在服务空闲时定时统计当天所有信息数据等。
定时任务有个非常流行的框架Quartz和Java原生API的Timer类。Spring框架也可以支持定时任务。

cron表达式

语法:秒 分 时 日 月 周 年(Spring 不支持)
http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

分布式定时任务
特殊字符:
分布式定时任务

,:枚举: (cron=“7,9,23 * * * * ?”):任意时刻的 7,9,23 秒启动这个任务;
-:范围: (cron=“7-20 * * * * ?”):任意时刻的 7-20 秒之间,每秒启动一次
()* :任意;指定位置的任意时刻都可以
/:步长; (cron=“7/5 * * * * ?”):第 7 秒启动,每 5 秒一次;(cron=“/5 * * * * ?“):任意秒启动,每 5 秒一次;
?:(出现在日和周几的位置):为了防止日和周冲突,在周和日上如果要写通配符使用?(cron=”
* * 1 * ?”):每月的 1 号,启动这个任务;
L:(出现在日和周的位置)”, last:最后一个 (cron=“* * * ? * 3L”):每月的最后一个周二
W:Work Day:工作日 (cron=“* * * W * ?”):每个月的工作日触发 (cron=“* * * LW * ?”):每个月的最后一个工作日触发
#:第几个 (cron=“* * * ? * 5#2”):每个月的第 2 个周 4

注意:日和周几通常会发生冲突,如果日定了,那么周就要使用?通配符

可以在线生成cron表达式
https://cron.qqe2.com/

cron实例
分布式定时任务

SpringBoot整合

 * 1、Spring中6位组成,不允许第7位的年* 2、在周几的位置,1-7代表周一到周日:MON-SUN

测试代码如下

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/* 定时任务*         1、@EnableScheduling开启定时任务*         2、@scheduled 开启一个定时任务* /
@EnableScheduling
@Component
@Slf4j
public class HelloSchedule {@Scheduled(cron = "*/5 * * * * 4")public void hello(){log.info("hello.....");try {Thread.sleep(8000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

但是这里存在一个问题

定时任务不应该阻塞,默认是阻塞的。(也就是要等业务执行完,定时任务才能开始执行)
分布式定时任务
可以使用以下几种解决方案
1)、CompletableFuture.runAsync()可以让业务以异步的方式,自己提交到线程
2)、支持定时任务线程池设置:

spring.task.scheduling.pool.size=5

3)、让定时任务异步执行
通常是采用第3种方案
开启异步定时任务

  • 异步任务
    1、@EnableAsycn:开启异步任务功能
    2、@Async 给希望异步执行的方法上标注上注解
    3、自动配置类 TaskExecutionAutoConfiguration 属性绑定在TaskExecutionProperties
    4、.配置定时任务的线程池属性:
spring.task.execution.pool.core-size=5
spring.task.execution.pool.max-size=30
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;/* 定时任务*         1、@EnableScheduling开启定时任务*         2、@scheduled 开启一个定时任务* 异步任务*         1、@EnableAsycn:开启异步任务功能*         2、@Async 给希望异步执行的方法上标注上注解*         3、自动配置类 TaskExecutionAutoConfiguration 属性绑定在TaskExecutionProperties*         4.配置:*              spring.task.execution.pool.core-size=5*              spring.task.execution.pool.max-size=30* /
@EnableScheduling
@Component
@Slf4j
@EnableAsync
public class HelloSchedule {/* 1、Spring中6位组成,不允许第7位的年* 2、在周几的位置,1-7代表周一到周日:MON-SUN* 3、定时任务不应该阻塞,默认是阻塞的。(也就是要等业务执行完,定时任务才能开始执行)*      1)、CompletableFuture.runAsync()可以让业务以异步的方式,自己提交到线程池*      2)、支持定时任务线程池设置:*          spring.task.scheduling.pool.size=5*      3)、让定时任务异步执行     解决:使用异步+定时任务来完成定时任务不阻塞的功能。* /@Async@Scheduled(cron = "*/5 * * * * 4")public void hello(){log.info("hello.....");try {Thread.sleep(8000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}