zoukankan      html  css  js  c++  java
  • Springboot使用Schedule实现定时任务

      现在在Springboot项目中,我们常见的实现定时任务的技术方案有两种,一种是使用quartz,quartz是一种完全由java 编写的开源作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。另外一种就是Schedule来实现定时任务。本篇文章主要记录一下使用Schedule的用法:

    案例
    1. 新建项目
      在这里插入图片描述
    2. 启用定时任务,添加注解@EnableScheduling在这里插入图片描述
    3. 创建定时任务
      @scheduled(cron=“0/2 * * * * *”)表示每2秒执行一次定时任务
      在这里插入图片描述
      代码:
    @Component
    public class ScheduleTest {
    
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    	@Scheduled(cron = "0/2 * * * * *")
    	public void print() {
    		System.out.println("这是定时任务1》》》》》"+sdf.format(new Date()));
    	}
    
    }
    

    4.启动项目
    在这里插入图片描述

    指定执行时间
    @Component
    public class ScheduleTest {
    
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    	@Scheduled(cron = "0/2 * * * * *")
    	public void print1() {
    		System.out.println("这是定时任务1》》》》》" + sdf.format(new Date()));
    	}
    
    	@Scheduled(fixedDelay = 2000)
    	public void print2() {
    		System.out.println("这是定时任务2》》》》》" + sdf.format(new Date()));
    	}
    
    	@Scheduled(fixedRate = 2000)
    	public void print3() {
    		System.out.println("这是定时任务3》》》》》" + sdf.format(new Date()));
    	}
    
    	@Scheduled(initialDelay = 2000)
    	public void print4() {
    		System.out.println("这是定时任务4》》》》》" + sdf.format(new Date()));
    	}
    
    }
    

    在上面贴出的代码中,分别使用了@Scheduled注解中的4个属性来指定定时任务执行的时间:

    fixedDelay:任务执行之间的时间间隔,具体是指本次定时任务结束到下次定时任务开始之间的间隔。
    fixedRate:任务执行之间的时间间隔,具体是指两次定时任务的开始时间间隔,第二次任务开始时,第一次定时任务可能还没有结束。
    initialDelay:表示首次任务启动的延迟时间
    cron:cron表达式是目前比较常用的一种指定任务执行时间的方式,特别是针对一些个性化的执行时间,使用cron表达式再合适不过。

    下面附上一些常见的cron表达式:

       (1)0 0 2 1 * ? *   表示在每月的1日的凌晨2点调整任务
    
      (2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业
    
      (3)0 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
    
      (4)0 0 10,14,16 * * ?   每天上午10点,下午2点,4点 
    
      (5)0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 
    
      (6)0 0 12 ? * WED    表示每个星期三中午12点 
    
      (7)0 0 12 * * ?   每天中午12点触发 
    
      (8)0 15 10 ? * *    每天上午10:15触发 
    
      (9)0 15 10 * * ?     每天上午10:15触发 
    
      (10)0 15 10 * * ? *    每天上午10:15触发 
    
      (11)0 15 10 * * ? 2005    2005年的每天上午10:15触发 
    
      (12)0 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发 
    
      (13)0 0/5 14 * * ?    在每天下午2点到下午2:55期间的每5分钟触发 
    
      (14)0 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
    
      (15)0 0-5 14 * * ?    在每天下午2点到下午2:05期间的每1分钟触发 
    
      (16)0 10,44 14 ? 3 WED    每年三月的星期三的下午2:10和2:44触发 
    
      (17)0 15 10 ? * MON-FRI    周一至周五的上午10:15触发 
    
      (18)0 15 10 15 * ?    每月15日上午10:15触发 
    
      (19)0 15 10 L * ?    每月最后一日的上午10:15触发 
    
      (20)0 15 10 ? * 6L    每月的最后一个星期五上午10:15触发 
    
      (21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发 
    
      (22)0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发
    
    多线程执行

    在这里插入图片描述
    当我们在任务类中创建了多个定时任务的时候,启动项目执行任务,发现不同的任务是跑的同一个线程,查看两个任务的执行时间,原本任务2是2秒执行一次(cron表达式1s+sleep 1秒),但是发现有3秒执行一次的情况,这对于执行时间有严格要求的任务来说肯定是不行的。

    在这里插入图片描述
    针对于此种情况,我们可以配置线程池来解决多个定时任务使用同一个线程来跑的情况。新建一个config包,下面新建一个定时任务配置文件,实现SchedulingConfigurer类,重写里面的configureTasks方法。

    在这里插入图片描述
    代码:

    @Configuration
    @EnableScheduling
    public class ScheduleConfig implements SchedulingConfigurer {
    
    	@Override
    	public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
    		scheduledTaskRegistrar.setScheduler(taskExcutor());
    	}
    
    	@Bean(destroyMethod = "shutdown")
    	public Executor taskExcutor() {
    		return Executors.newScheduledThreadPool(10);
    	}
    }
    

    然后再重启项目,查看定时任务打印出来的时间:

    在这里插入图片描述
    发现两个任务的打印时间严格执行我们指定的时间(cron指定的执行时间+sleep线程睡眠时间)。

  • 相关阅读:
    【SCOI 2011】 糖果
    【POJ 3159】 Candies
    【POJ 1716】 Integer Intervals
    【POJ 2983】 Is the information reliable?
    【POJ 1364】 King
    【POJ 1201】 Intervals
    【POJ 1804】 Brainman
    6月10日省中提高组题解
    【POJ 3352】 Road Construction
    【POJ 1144】 Network
  • 原文地址:https://www.cnblogs.com/wgty/p/12810485.html
Copyright © 2011-2022 走看看