springboot中定时任务的使用

1.在springboot启动器开启定时任务注解

在这里插入图片描述

这里的 @EnableScheduling 注解,它的作用是发现注解 @Scheduled的任务并由后台执行。没有它的话将无法执行定时任务。

2.使用的@Scheduled 注解

2.1 @Scheduled 的常用配置项

@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行,无论上一个任务是否执行完过2s就开始下一个任务;

@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行,上一个任务执行完后过2s 才执行下个任务;

@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,
然后在上一次执行完毕时间点后2秒再次执行;

@Scheduled(cron=“* * * * * ?”):按cron规则执行。
在线Cron表达式生成器:http://cron.qqe2.com/介绍

3 实战

3.1 @Scheduled 多线程并发执行

@Scheduled 默认是使用单线程执行所有任务的。

1)验证:

编写多个@Scheduled 任务且设置执行时间和时间间隔都相同。

@Component
public class AnnotationSchedulTask {
    private static final Logger LOG = LoggerFactory.getLogger(AnnotationSchedulTask.class);
    private int count=0;
    
    @Scheduled(cron = "0/2 * * * * ? ")
    public  void test(){
        //0/1 第一次运行间隔0s,每次间隔1s运行一次
        LOG.info("SchedulTask run count:"+count++);
    }
    
    /**
     * 测试@Scheduled 默认是否是单线程运行
     */
    @Scheduled(cron = "0/2 * * * * ? ")
    public  void testConcurrence(){
        LOG.info("*********testConcurrence run**************");
    }

    /**
     * 测试@Scheduled 默认是否是单线程运行
     */
    @Scheduled(cron = "0/2 * * * * ? ")
    public  void testConcurrence2(){
        LOG.info("--------testConcurrence2-----------");
    }
    
}

启动服务运行结果:

可以看到所有任务都被 scheduling-1 线程执行
在这里插入图片描述

原因:

从ScheduledAnnotationBeanPostProcessor类开始一路找下去,在ScheduledTaskRegistrar(定时任务注册类)中的ScheduleTasks中又这样一段判断:

if (this.taskScheduler == null) {
 this.localExecutor = Executors.newSingleThreadScheduledExecutor();
 this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
}

当this.taskScheduler 为null 时
Executors.newSingleThreadScheduledExecutor(); 单个线程的线程池。

2)扩展多线程
其实只要设置一个我们自定义的this.taskScheduler

方法一:
配置一个ScheduledExecutorService bean即可设置

 /**
     * 执行周期性或定时任务
     */
    @Bean(name = "scheduledExecutorService")
    protected ScheduledExecutorService scheduledExecutorService()
    {
        return new ScheduledThreadPoolExecutor(corePoolSize,
                new BasicThreadFactory.Builder().namingPattern("MySchedule-pool-%d").daemon(true).build(),
                new ThreadPoolExecutor.CallerRunsPolicy())
        {
            @Override
            protected void afterExecute(Runnable r, Throwable t)
            {
                super.afterExecute(r, t);
                if (t == null && r instanceof Future<?>)
                {
                    try
                    {
                        Future<?> future = (Future<?>) r;
                        if (future.isDone())
                        {
                            future.get();
                        }
                    }
                    catch (CancellationException ce)
                    {
                        t = ce;
                    }
                    catch (ExecutionException ee)
                    {
                        t = ee.getCause();
                    }
                    catch (InterruptedException ie)
                    {
                        Thread.currentThread().interrupt();
                    }
                }
                if (t != null)
                {
                    log.error(t.getMessage(), t);
                }
            }
        };
    }

执行结果:

可以看到现在@Scheduled 由不同线程执行。
在这里插入图片描述

原理:

ScheduledAnnotationBeanPostProcessor 类的finishRegistration方法

最终在 this.registrar.setScheduler(resolveSchedulerBean(this.beanFactory, ScheduledExecutorService.class, false));

这一行 我们自定义的ScheduledExecutorService bean被当作执行的线程池。

方法2:

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler( new ScheduledThreadPoolExecutor(8,
                new BasicThreadFactory.Builder().namingPattern("taskRegistrar-pool-%d").daemon(true).build(),
                new ThreadPoolExecutor.CallerRunsPolicy()));
    }
}

运行结果:

可以看出来也成功了,并且覆盖了我们之前设置的ScheduledExecutorService。
在这里插入图片描述

3.2 cron 表达式实现指定时间执行任务

1)cron 表达式详细介绍:

域 必填 允许值 允许的通配符
秒(seconds) 是 0-59整数 , - * /
分(minutes) 是 0-59整数 , - * /
时(hours) 是 0-23整数 , - * /
日(daysOfMonth) 是 1-31整数(需要考虑月的天数) , - * ? / L W
月(months) 是 1-12整数 或 JAN-DEC , - * /
周 (daysOfWeek) 是 1-7整数 或 SUN-SAT , - * ? / L #
通配符说明:
*:表示匹配该域的任意值。在minutes域使用 * 表示每分钟。在months里表示每个月。在daysOfWeek域表示一周的每一天。
?:只能用在daysofMonth和daysofWeek两个域,表示不指定值,当两个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为 ?。因为daysofMonth和daysofWeek会相互影响。例如想在每月的2号触发调度,不管2号是周几,则只能使用如下写法:0 0 0 2 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管周几都会触发。
-:表示范围。例如在minutes域使用5-20,表示从5分到20分钟每分钟触发一次
/:表示起始时间开始触发,然后每隔固定时间触发一次。例如在minutes域使用5/20,则意味着从当前小时的第5分钟开每20分钟触发一次。
,:表示列出枚举值。例如:在minutes域使用5,20,则意味着在5分和20分时各触发一次。
L:表示最后,是单词“last”的缩写,只能出现在daysofWeek和dayofMonth域。在daysofWeek域使用5L意思是在指定月的最后的一个星期四触发。在dayofMonth域使用5L或者FRIL意思是在指定月的倒数第5天触发。在使用L参数时,不要指定列表或范围。
W:表示有效工作日(周一到周五),只能出现在daysofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在daysofMonth使用5W,如果5号是周六,则将在最近的工作日周五,即4号触发。如果5号是周日,则在6(周一)触发。如果5日在星期一到星期五中的一天,则就在5日触发。另外,W的最近寻找不会跨过月份 。
LW:这两个字符可以连用,表示指定月的最后一个工作日。
#:用于确定每个月第几个周几,只能出现在daysOfWeek域。例如在4#2,表示某月的第二个周三。
常用表达式示例:
0/2 * * * * ? 表示每2秒 执行任务
0 0/2 * * * ? 表示每2分钟 执行任务
0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
0 0 10,14,16 * * ? 每天上午10点,下午2点,40 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午120 0 12 * * ? 每天中午12点触发
0 15 10 ? * * 每天上午10:15触发
0 15 10 * * ? 每天上午10:15触发
0 15 10 * * ? 每天上午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:102:44触发
0 15 10 ? * MON-WED,SAT 周一至周三和周六的上午10:15触发
0 15 10 15 * ? 每月15日上午10:15触发
0 15 10 L * ? 每月最后一日的上午10:15触发
0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

2)设置每天固定时间执行

    //测试每天下午两点执行
    @Scheduled(cron = "0 0 14 * * ? ")
    public  void testCron2(){
        LOG.info("*********testCron2 run**************");
    }

结果:

在下午两点被执行

14:00:00.005 [taskRegistrar-pool-1] INFO  c.e.s.api.task.AnnotationSchedulTask - *********testCron2 run**************```


**3)设定没1分钟执行一次**

```java
   //测试每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ? ")
    public  void testFixedDelay(){
        LOG.info("*********testFixedDelay run**************");
    }

结果:

每分钟执行一次
在这里插入图片描述

4)设定每月几号执行一次

必须指定执行时间不然当天每分钟都会被执行一次。

  //设定每月几号执行一次
    @Scheduled(cron = "0 14 14 3 * ? ")
    public  void testFixedDay(){
        LOG.info("*********testFixedDay run**************");
    }

结果:

在 3号的 14.14 被执行。
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值