分布式定时任务

29 篇文章 0 订阅
12 篇文章 0 订阅

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

定时任务

定时任务是我们系统里面经常要用到的一些功能。如每天的支付订单要与支付宝进行对账操作、每个月定期进行财务汇总、在服务空闲时定时统计当天所有信息数据等。
定时任务有个非常流行的框架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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值