一句话说spring task
spring task 是一个可以让你的程序在你规定的时机执行规定好的动作,如同古人云:万事俱备只欠东风,那么东风来时即可一战。
程序中理解
spring task 在spring-core包下,我们只需引入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
准备好要执行的任务(作战计划)
package com.lagersoft.task;
import java.time.LocalDateTime;
import org.springframework.stereotype.Component;
/**
* 调度任务
* @author xiaoping
*
*/
@Component
public class FirstTask {
public void execute1(){
System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now());
}
public void execute2(){
System.out.printf("Task: %s, Current time: %s\n", 2, LocalDateTime.now());
}
public void execute3(){
System.out.printf("Task: %s, Current time: %s\n", 3, LocalDateTime.now());
}
public void execute4(){
System.out.printf("Task: %s, Current time: %s\n", 4, LocalDateTime.now());
}
public void execute5(){
System.out.printf("Task: %s, Current time: %s\n", 5, LocalDateTime.now());
}
public void execute6(){
System.out.printf("Task: %s, Current time: %s\n", 6, LocalDateTime.now());
}
public void execute7(){
System.out.printf("Task: %s, Current time: %s\n", 7, LocalDateTime.now());
}
public void execute8(){
System.out.printf("Task: %s, Current time: %s\n", 8, LocalDateTime.now());
}
public void execute9(){
System.out.printf("Task: %s, Current time: %s\n", 9, LocalDateTime.now());
}
public void execute10(){
System.out.printf("Task: %s, Current time: %s\n", 10, LocalDateTime.now());
}
public void execute11(){
System.out.printf("Task: %s, Current time: %s\n", 11, LocalDateTime.now());
}
}
spring配置文件(配置好作战时机)
<!-- 配置注解扫描 -->
<context:component-scan base-package="com.lagersoft.task"/>
<task:scheduler id="taskScheduler" pool-size="100" />
<!--cron 表达式 : {秒} {分} {时} {日期(具体哪天)} {月} {星期} 6个参数,空格隔开 -->
<!-- ref 关联到实现类FirstTask method 关联到具体执行的方法 cron 则指明该方法的执行时机 -->
<task:scheduled-tasks scheduler="taskScheduler">
<!-- 每分钟的30秒时触发任务 -->
<task:scheduled ref="firstTask" method="execute1" cron="30 * * * * ?"/>
<!-- 每小时的30分钟时触发任务 -->
<task:scheduled ref="firstTask" method="execute2" cron="0 30 * * * ?"/>
<!-- 每天12点钟触发任务 -->
<task:scheduled ref="firstTask" method="execute3" cron="0 0 12 * * ?"/>
<!-- 每月15号的3点40分时触发任务,比如说计算员工工资,嘻嘻 -->
<task:scheduled ref="firstTask" method="execute4" cron="0 40 3 15 * ?"/>
<!-- 每年10月1号的8点10分时触发任务 比如说国庆节给员工发福利 -->
<task:scheduled ref="firstTask" method="execute5" cron="0 10 8 1 10 ?"/>
<!-- 每分钟的15秒、30秒、45秒时触发任务 -->
<task:scheduled ref="firstTask" method="execute6" cron="15,30,45 * * * * ?"/>
<!-- 每分钟的15秒到45秒每隔1秒触发任务 -->
<task:scheduled ref="firstTask" method="execute7" cron="15-45 * * * * ?"/>
<!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 -->
<task:scheduled ref="firstTask" method="execute8" cron="15/5 * * * * ?"/>
<!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 -->
<task:scheduled ref="firstTask" method="execute9" cron="15-30/5 * * * * ?"/>
<!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 -->
<task:scheduled ref="firstTask" method="execute10" cron="0 0/3 * * * ?"/>
<!-- 星期一到星期五的9点30分0秒触发任务 比如开始计算工作日员工考勤情况 -->
<task:scheduled ref="firstTask" method="execute11" cron="0 30 9 ? * MON-FRI"/>
</task:scheduled-tasks>
启动程序(开始暗中观察)
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/root-context.xml");
}
最后
再来看spring task 的时候,你可能会认为它是一个闹钟或是定时器,啥也不说,code里面出真知(专属于你的认知)