springboot 实现定时任务(Schedule)

1.主启动类(@EnableScheduling)
@SpringBootApplication
@EnableScheduling // 开启定时任务功能
@EnableAsync // 开启多线程定时任务
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
2.定时任务实现方式
1.注解(@Scheduled)
package com.qin.test;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @desciption: 注解实现定时任务
 * @author: wangzhibin
 * @date: 2020/10/16 10:41
 * @version: V1.0.0
 * @className: ScheduledTest
 */
@Component
public class ScheduledTest1 {

    // ${cron}: 从配置文件读取cron表达式
    @Scheduled(cron = "${cron}")
    public void test(){
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}
2.接口(实现SchedulingConfigurer)
package com.qin.test;

import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @desciption: 接口实现定时任务
 * @author: wangzhibin
 * @date: 2020/10/16 10:41
 * @version: V1.0.0
 * @className: ScheduledTest
 */
@Component
public class ScheduledTest2 implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("执行动态定时任务: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    }
                },
                //2.设置执行周期(Trigger)
                new Trigger() {
                    @Override
                    public Date nextExecutionTime(TriggerContext triggerContext) {
                        //2.1 从数据库动态获取cron表达式/或者根据业务需求从其它地方获取
                        String cron = "*/5 * * * * ?";
                        //2.2 校验cron表达式是否合法.
                        if (StringUtils.isEmpty(cron)) {
                            // Omitted Code ..
                        }
                        //2.3 返回执行周期(Date)
                        return new CronTrigger(cron).nextExecutionTime(triggerContext);
                    }
                }
        );
    }
}
3.多线程(@Async:可以标注在类和方法上)
package com.qin.test;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @desciption: 多线程实现定时任务
 * @author: wangzhibin
 * @date: 2020/10/20 15:41
 * @version: V1.0.0
 * @className: ScheduledTest3
 */
@Component
@Async
public class ScheduledTest3 {

    // ${cron}: 从配置文件读取cron表达式
    @Scheduled(cron = "*/3 * * * * ?")
    @Async
    public void test(){
        System.out.println("第一个任务:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    @Scheduled(cron = "*/1 * * * * ?")
    public void test2(){
        System.out.println("第二个任务:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}

3.cron表达式

在线工具:https://qqe2.com/cron/index

1.表格
位置时间单位值(数字或字母)值(特殊字符)
10-59, - * /
2分钟0-59, - * /
3小时0-23, - * /
41-31, - * / L W C
51-12, - * /
6星期1-7 (注意1是星期天)SUN,MON,TUE,WED,THU,FRI,SAT, - * / ? L W C
7年(可选)1970-2099, - * /
2.特殊字符说明
  1. 星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;
  2. 问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于占位符;
  3. 减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;
  4. 逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;
  5. 斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在秒数字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;
  6. 井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;
  7. L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;
  8. W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;
  9. LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;
  10. C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。
3.示例
  • “*/5 * * ?” 每隔5秒执行一次:
  • ”0 */1 * ?“ 每隔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触发
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值