spring task定时调度任务

素颜马尾好姑娘i 2022-06-11 06:13 361阅读 0赞

深入浅出spring task定时任务

在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutorTaskScheduler这两个接口的方式为异步定时任务提供了一种抽象。这就意味着spring容许你使用其他的定时任务框架,当然spring自身也提供了一种定时任务的实现:spring task。spring task支持线程池,可以高效处理许多不同的定时任务。同时,spring还支持使用Java自带的Timer定时器和Quartz定时框架。限于篇幅,这里将只介绍spring task的使用。

其实,官方文档已经介绍地足够详细,只不过都是英文版,所以为了更好地理解并使用spring task,首先会对spring task的实现原理做一个简单的介绍,然后通过实际代码演示spring task是如何使用的。这里会涉及到一个很重要的知识点:cron表达式。

TaskExecutor和TaskScheduler

TaskExecutor是spring task的第一个抽象,它很自然让人联想到jdk中concurrent包下的Executor,实际上TaskExecutor就是为区别于Executor才引入的,而引入TaskExecutor的目的就是为定时任务的执行提供线程池的支持,那么,问题来了,为什么spring不直接使用jdk自带的Executor呢?TaskExecutor源码如下?

  1. public interface TaskExecutor extends Executor {
  2. void execute(Runnable var1);
  3. }
  4. 1
  5. 2
  6. 3
  7. 1
  8. 2
  9. 3

那么,答案很显然,TaskExecutor提供的线程池支持也是基于jdk自带的Executor的。用法于Executor没有什么不同。

TaskScheduler是spring task的第二个抽象,那么从字面的意义看,TaskScheduler就是为了提供定时任务的支持咯。TaskScheduler需要传入一个Runnable的任务做为参数,并指定需要周期执行的时间或者触发器,这样Runnable任务就可以周期性执行了。传入时间很好理解,有意思的是传入一个触发器(Trigger)的情况,因为这里需要使用cron表达式去触发一个定时任务,所以有必要先了解下cron表达式的使用。

在spring 4.x中已经不支持7个参数的cronin表达式了,要求必须是6个参数(具体哪个参数后面会说)。cron表达式的格式如下:

  1. { 秒} { 分} { 时} { 日期(具体哪天)} { 月} { 星期}
  2. 1
  3. 1
  • 秒:必填项,允许的值范围是0-59,支持的特殊符号包括, - * /,表示特定的某一秒才会触发任务,-表示一段时间内会触发任务,*表示每一秒都会触发,/表示从哪一个时刻开始,每隔多长时间触发一次任务。
  • 分:必填项,允许的值范围是0-59,支持的特殊符号和秒一样,含义类推
  • 时:必填项,允许的值范围是0-23,支持的特殊符号和秒一样,含义类推
  • 日期:必填项,允许的值范围是1-31,支持的特殊符号相比秒多了?,表示与{星期}互斥,即意味着若明确指定{星期}触发,则表示{日期}无意义,以免引起冲突和混乱。
  • 月:必填项,允许的值范围是1-12(JAN-DEC),支持的特殊符号与秒一样,含义类推
  • 星期:必填项,允许值范围是1~7 (SUN-SAT),1代表星期天(一星期的第一天),以此类推,7代表星期六,支持的符号相比秒多了?,表达的含义是与{日期}互斥,即意味着若明确指定{日期}触发,则表示{星期}无意义。

比如下面这个cron表达式:

  1. // 表达的含义是:每半分钟触发一次任务
  2. 30 * * * * ?
  3. 1
  4. 2
  5. 1
  6. 2

spring提供了一个CronTrigger,通过传入一个Runnable任务和CronTrigger,就可以使用cron表达式去指定定时任务了,是不是非常方面。实际上,在工程实践上,cron表达式也是使用很多的。实际上,是执行了下面的代码:

  1. scheduler.schedule(task, new CronTrigger("30 * * * * ?"));
  2. 1
  3. 1

TaskScheduler抽象的好处是让需要执行定时任务的代码不需要指定特定的定时框架(比如Timer和Quartz)。TaskScheduler的更简单的实现是ThreadPoolTaskScheduler,它实际上代理一个jdk中的SchedulingTaskExecutor,并且也实现了TaskExecutor接口,所以需要经常执行定时任务的场景可以使用这个实现(Spring推荐)。我们再来看一下TaskExecutor和TaskScheduler的类继承关系:

spring-task

通常而言,使用spring task实现定时任务有两种方式:注解和xml配置文件。这里使用xml配置文件的方式加以说明。

实战

创建Maven工程,pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.rhwayfun</groupId>
  5. <artifactId>sring-task-demo</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>4.2.4.RELEASE</version>
  12. </dependency>
  13. </dependencies>
  14. <build>
  15. <plugins>
  16. <plugin>
  17. <groupId>org.apache.maven.plugins</groupId>
  18. <artifactId>maven-compiler-plugin</artifactId>
  19. <version>3.5.1</version>
  20. <configuration>
  21. <source>1.8</source>
  22. <target>1.8</target>
  23. </configuration>
  24. </plugin>
  25. </plugins>
  26. </build>
  27. </project>
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. 8
  36. 9
  37. 10
  38. 11
  39. 12
  40. 13
  41. 14
  42. 15
  43. 16
  44. 17
  45. 18
  46. 19
  47. 20
  48. 21
  49. 22
  50. 23
  51. 24
  52. 25
  53. 26
  54. 27
  55. 28
  56. 29
  57. 30
  58. 31
  59. 32
  60. 1
  61. 2
  62. 3
  63. 4
  64. 5
  65. 6
  66. 7
  67. 8
  68. 9
  69. 10
  70. 11
  71. 12
  72. 13
  73. 14
  74. 15
  75. 16
  76. 17
  77. 18
  78. 19
  79. 20
  80. 21
  81. 22
  82. 23
  83. 24
  84. 25
  85. 26
  86. 27
  87. 28
  88. 29
  89. 30
  90. 31
  91. 32

开发需要执行定时任务的方法:

  1. package com.rhwayfun.task;
  2. import org.springframework.stereotype.Component;
  3. import java.time.LocalDateTime;
  4. /** * @author ZhongCB * @date 2016年09月10日 14:30 * @description */
  5. @Component
  6. public class App {
  7. public void execute1(){
  8. System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now());
  9. }
  10. public void execute2(){
  11. System.out.printf("Task: %s, Current time: %s\n", 2, LocalDateTime.now());
  12. }
  13. public void execute3(){
  14. System.out.printf("Task: %s, Current time: %s\n", 3, LocalDateTime.now());
  15. }
  16. public void execute4(){
  17. System.out.printf("Task: %s, Current time: %s\n", 4, LocalDateTime.now());
  18. }
  19. public void execute5(){
  20. System.out.printf("Task: %s, Current time: %s\n", 5, LocalDateTime.now());
  21. }
  22. public void execute6(){
  23. System.out.printf("Task: %s, Current time: %s\n", 6, LocalDateTime.now());
  24. }
  25. public void execute7(){
  26. System.out.printf("Task: %s, Current time: %s\n", 7, LocalDateTime.now());
  27. }
  28. public void execute8(){
  29. System.out.printf("Task: %s, Current time: %s\n", 8, LocalDateTime.now());
  30. }
  31. public void execute9(){
  32. System.out.printf("Task: %s, Current time: %s\n", 9, LocalDateTime.now());
  33. }
  34. public void execute10(){
  35. System.out.printf("Task: %s, Current time: %s\n", 10, LocalDateTime.now());
  36. }
  37. public void execute11(){
  38. System.out.printf("Task: %s, Current time: %s\n", 11, LocalDateTime.now());
  39. }
  40. }
  41. 1
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. 7
  48. 8
  49. 9
  50. 10
  51. 11
  52. 12
  53. 13
  54. 14
  55. 15
  56. 16
  57. 17
  58. 18
  59. 19
  60. 20
  61. 21
  62. 22
  63. 23
  64. 24
  65. 25
  66. 26
  67. 27
  68. 28
  69. 29
  70. 30
  71. 31
  72. 32
  73. 33
  74. 34
  75. 35
  76. 36
  77. 37
  78. 38
  79. 39
  80. 40
  81. 41
  82. 42
  83. 43
  84. 44
  85. 45
  86. 46
  87. 47
  88. 48
  89. 49
  90. 50
  91. 51
  92. 52
  93. 53
  94. 54
  95. 55
  96. 56
  97. 57
  98. 58
  99. 59
  100. 60
  101. 1
  102. 2
  103. 3
  104. 4
  105. 5
  106. 6
  107. 7
  108. 8
  109. 9
  110. 10
  111. 11
  112. 12
  113. 13
  114. 14
  115. 15
  116. 16
  117. 17
  118. 18
  119. 19
  120. 20
  121. 21
  122. 22
  123. 23
  124. 24
  125. 25
  126. 26
  127. 27
  128. 28
  129. 29
  130. 30
  131. 31
  132. 32
  133. 33
  134. 34
  135. 35
  136. 36
  137. 37
  138. 38
  139. 39
  140. 40
  141. 41
  142. 42
  143. 43
  144. 44
  145. 45
  146. 46
  147. 47
  148. 48
  149. 49
  150. 50
  151. 51
  152. 52
  153. 53
  154. 54
  155. 55
  156. 56
  157. 57
  158. 58
  159. 59
  160. 60

spring配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
  3. <!-- 配置注解扫描 -->
  4. <context:component-scan base-package="com.rhwayfun.task"/>
  5. <task:scheduler id="taskScheduler" pool-size="100" />
  6. <task:scheduled-tasks scheduler="taskScheduler">
  7. <!-- 每半分钟触发任务 -->
  8. <task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/>
  9. <!-- 每小时的10分30秒触发任务 -->
  10. <task:scheduled ref="app" method="execute2" cron="30 10 * * * ?"/>
  11. <!-- 每天1点10分30秒触发任务 -->
  12. <task:scheduled ref="app" method="execute3" cron="30 10 1 * * ?"/>
  13. <!-- 每月20号的1点10分30秒触发任务 -->
  14. <task:scheduled ref="app" method="execute4" cron="30 10 1 20 * ?"/>
  15. <!-- 每年10月20号的1点10分30秒触发任务 -->
  16. <task:scheduled ref="app" method="execute5" cron="30 10 1 20 10 ?"/>
  17. <!-- 每15秒、30秒、45秒时触发任务 -->
  18. <task:scheduled ref="app" method="execute6" cron="15,30,45 * * * * ?"/>
  19. <!-- 15秒到45秒每隔1秒触发任务 -->
  20. <task:scheduled ref="app" method="execute7" cron="15-45 * * * * ?"/>
  21. <!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 -->
  22. <task:scheduled ref="app" method="execute8" cron="15/5 * * * * ?"/>
  23. <!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 -->
  24. <task:scheduled ref="app" method="execute9" cron="15-30/5 * * * * ?"/>
  25. <!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 -->
  26. <task:scheduled ref="app" method="execute10" cron="0 0/3 * * * ?"/>
  27. <!-- 星期一到星期五的10点15分0秒触发任务 -->
  28. <task:scheduled ref="app" method="execute11" cron="0 15 10 ? * MON-FRI"/>
  29. </task:scheduled-tasks>
  30. </beans>
  31. 1
  32. 2
  33. 3
  34. 4
  35. 5
  36. 6
  37. 7
  38. 8
  39. 9
  40. 10
  41. 11
  42. 12
  43. 13
  44. 14
  45. 15
  46. 16
  47. 17
  48. 18
  49. 19
  50. 20
  51. 21
  52. 22
  53. 23
  54. 24
  55. 25
  56. 26
  57. 27
  58. 28
  59. 29
  60. 30
  61. 31
  62. 32
  63. 33
  64. 34
  65. 35
  66. 36
  67. 37
  68. 38
  69. 39
  70. 40
  71. 41
  72. 42
  73. 1
  74. 2
  75. 3
  76. 4
  77. 5
  78. 6
  79. 7
  80. 8
  81. 9
  82. 10
  83. 11
  84. 12
  85. 13
  86. 14
  87. 15
  88. 16
  89. 17
  90. 18
  91. 19
  92. 20
  93. 21
  94. 22
  95. 23
  96. 24
  97. 25
  98. 26
  99. 27
  100. 28
  101. 29
  102. 30
  103. 31
  104. 32
  105. 33
  106. 34
  107. 35
  108. 36
  109. 37
  110. 38
  111. 39
  112. 40
  113. 41
  114. 42

编写测试代码:

  1. package com.rhwayfun.task;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. /** * @author ZhongCB * @date 2016年09月10日 14:55 * @description */
  5. public class AppTest {
  6. public static void main(String[] args) {
  7. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/app-context-task.xml");
  8. }
  9. }
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10
  20. 11
  21. 12
  22. 13
  23. 14
  24. 15
  25. 16
  26. 1
  27. 2
  28. 3
  29. 4
  30. 5
  31. 6
  32. 7
  33. 8
  34. 9
  35. 10
  36. 11
  37. 12
  38. 13
  39. 14
  40. 15
  41. 16

运行测试代码,控制台会定时输出每个定时任务的日志信息,说明测试通过。

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读