SpringTask

本贴最后更新于 741 天前,其中的信息可能已经时异事殊

SpringTask

一、定时任务

定时任务的应用场景

  • 月底会员扣费
  • 会员到期
  • 生日祝福
  • 等等

二、@Scheduled()参数

定义在方法上

public @interface Scheduled {
    String cron() default "";				//表达式,指定任务在特定时间执行;
    String zone() default "";           	//时区,默认为当前时区。
    long fixedDelay() default -1;			//表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
    String fixedDelayString() default "";	//与fixeddelay含义一样,只是参数类型变为string;
    long fixedRate() default -1;			//表示按一定的频率执行任务,即每次开始执行的时间间隔一致,参数类型为long,单位ms;
    String fixedRateString() default "";	//与fixedrate的含义一样,只是将参数类型变为string;
    long initialDelay() default -1;			//表示延迟多久再第一次执行任务,参数类型为long,单位ms;
    String initialDelayString() default "";	//与initialdelay的含义一样,只是将参数类型变为string;
    TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}

2.1 cron

  • [秒] [分] [时] [日] [月] [周]
  • spring 自带的定时任务 cron 表达式不支持年

如图:

android 开发分享关于 @Scheduled 参数及 cron 表达式解释

上图类中有如下源代码:

  private void parse(string expression) throws illegalargumentexception {  
      string[] fields = stringutils.tokenizetostringarray(expression, " ");  
      if (!arevalidcronfields(fields)) {  	
          throw new illegalargumentexception(string.format"cron expression must consist of 6 fields (found 				%d in "%s")", fields.length, expression));  
      }  
      doparse(fields);  }  
	private static boolean arevalidcronfields(string[] fields) {  
        return (fields != null && fields.length == 6);  
    }  
}

代码中 expression 参数即 cron 表达式,所以在 spring 自带的定时任务中正确的 cron 表达式只能是 6 个域,否则会抛出 illegalargumentexception 提示:cron expression must consist of 6 fields(cron 表达式必须由 6 个域组成)。

必填 允许值 允许的通配符
秒(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:这两个字符可以连用,表示指定月的最后一个工作日。
  • #:用于确定每个月第几个周几,只能出现在 daysofmonth 域。例如在 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 点,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 * 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-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 触发

@Scheduled("*/6 * * * * ?")   
public void test(){
        System.out.println("每隔六秒执行一次-->"+new Date());
}

生成器工具地址-http://cron.qqe2.com/

2.2 fixedDelay

@Scheduled(fixedDelay = 1000 * 10)  
public void test(){
        System.out.println("每隔10秒执行一次-->"+new Date());
}

2.3 fixedRate

@Scheduled(fixedRate = 1000 * 10)  
public void test(){
        System.out.println("每隔10秒执行一次-->"+new Date());
}

2.4initialDelay

@Scheduled(initialDelay = 1000, fixedRate = 3000)
public void test(){
        System.out.println("第一次延迟1s,之后按照fixedRate的规则执行--->"+new Date());
}

2.5 注意事项

1、定时器的参数有两种写法是用 cron 表达式,或者使用 fixeddelay、fixedrate 等参数直接配置

需要注意的是 使用 cron 表达式的方法,在项目首次启动后不会直接运行,而是等到执行周期才会执行

而使用第二种方式的定时器方法,在项目启动成功后会马上开始执行一次,再按照时间周期执行

2、定时器默认为单线程,所以如果项目中使用多个定时器要配置线程池

三、实战

3.1、创建一个 SpringBoot 项目

3.2、在启动类上添加注解

@EnableScheduling 开启 springtask

因为 SpringTask 是属于 Spring 内置的,所以无需再添加 pom 依赖

@SpringBootApplication
@EnableScheduling
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

3.3、新建测试类

在定时方法上添加@Scheduled()注解

@Component
public class OrderTImeCancelTask {

    @Scheduled(fixedDelay = 1000)
    public void test(){
        System.out.println("定时项目测试1---date-->"+new Date());
    }

    @Scheduled(fixedDelay = 1000)
    public void test2(){
        System.out.println("定时项目测试2---date-->"+new Date());
    }
}

3.4、启动项目

运行结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.4)

2022-04-15 21:16:26 [main] INFO  com.springcache.DemoApplication - Starting DemoApplication using Java 1.8.0_271 on LAPTOP-EH2KBQ07 with PID 163504 (E:\git项目\java普通项目\SpringCacheTest\target\classes started by admin in E:\git项目\java普通项目\SpringCacheTest)
2022-04-15 21:16:26 [main] INFO  com.springcache.DemoApplication - No active profile set, falling back to 1 default profile: "default"
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 11 ms. Found 0 JDBC repository interfaces.
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-04-15 21:16:27 [main] INFO  o.s.d.r.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 5 ms. Found 0 Redis repository interfaces.
2022-04-15 21:16:28 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.AprLifecycleListener - An older version [1.2.26] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.AprLifecycleListener - Loaded Apache Tomcat Native library [1.2.26] using APR version [1.7.0].
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.AprLifecycleListener - APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.AprLifecycleListener - APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.AprLifecycleListener - OpenSSL successfully initialized [OpenSSL 1.1.1i  8 Dec 2020]
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.StandardService - Starting service [Tomcat]
2022-04-15 21:16:28 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.58]
2022-04-15 21:16:28 [main] INFO  o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2022-04-15 21:16:28 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 1695 ms
2022-04-15 21:16:28 [main] INFO  com.springcache.Config.CacheConfig - 初始化 -> [Redis CacheErrorHandler]
2022-04-15 21:16:29 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2022-04-15 21:16:30 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2022-04-15 21:16:30 [main] INFO  o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
定时项目测试1---date-->Fri Apr 15 21:16:30 CST 2022
定时项目测试2---date-->Fri Apr 15 21:16:30 CST 2022
2022-04-15 21:16:30 [main] INFO  com.springcache.DemoApplication - Started DemoApplication in 4.368 seconds (JVM running for 6.444)
定时项目测试1---date-->Fri Apr 15 21:16:31 CST 2022
定时项目测试2---date-->Fri Apr 15 21:16:31 CST 2022
定时项目测试1---date-->Fri Apr 15 21:16:32 CST 2022
定时项目测试2---date-->Fri Apr 15 21:16:32 CST 2022
定时项目测试1---date-->Fri Apr 15 21:16:33 CST 2022
定时项目测试2---date-->Fri Apr 15 21:16:33 CST 2022

可以看到,只要项目启动,他就会一直执行

文章参考

参考文章 1

参考文章 2

  • 程序员

    程序员是从事程序开发、程序维护的专业人员。

    533 引用 • 3528 回帖

相关帖子

回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...