本文正在参加「Java主题月 - Java Debug笔记活动」,详情查看 活动链接
基于注解
首先在启动类中加上注解@EnableScheduling
@SpringBootApplication
@EnableScheduling //开启定时任务
public class TasksApplication {
public static void main(String[] args) {
SpringApplication.run(TasksApplication.class, args);
}
}
创建CronController类
@RestController
public class CronController {
//添加定时任务
@Scheduled(cron = "0/5 * * * * ?")
public void configureTasks() {
System.err.println("执行定时任务: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
}
然后启动项目,控制台就进行当前时间。"0/5 * * * * ?" 代表每隔五秒执行一次。
cron表达式
cron 一共有7位,最后一位是年,可以留空,所以我们可以写6位:
-
第一位,表示秒,取值0-59
-
第二位,表示分,取值0-59
-
第三位,表示小时,取值0-23
-
第四位,表示日,取值1-31
-
第五位,表示月份,取值1-12
-
第六位,表示星期/周,取值1-7,1表示星期天,2表示星期一。
-
第7为,表示年份,可以留空,取值1970-2099
其中最难理解的就是那些符号了,下面每个符号举个栗子:
通用符号
,:表示列出枚举值,例如在第二位使用5,35,表示在分钟数为5、35时执行。
-:表示范围,例如在第二位使用5-35,表示在分钟数为5到35内每分钟都执行。
*:表示匹配该域的任意值。例如在第二位使用*表示分钟数不做限制,每分钟都会执行。
/:表示起始时间开始执行,然后每隔固定时间执行一次。例如在第二位使用5/6,表示在分钟数为5时执行一次,然后隔6分钟执行一次,也就是在11、17分钟再分别执行。
专有符号
?:只能用在第四位(日)和第六位(星期)两个域,因为这两个域互斥,必须对其一设置?。
L:表示最后。只能用在第四位(日)和第六位(星期)两个域,如果在第六位使用 5L ,意味着在最后的一个星期四执行(1表示星期天,2表示星期一)。
W:表示有效工作日(周一到周五),只能出现在第四位(日)域。系统将在离指定日期最近的有效工作日触发事件。如15W,表示最接近15号的工作日,可能是15号(刚好是工作日)那就在15号执行。15号如果不是工作日,是星期天,那就往后推,在16号执行。16号是最接近工作日的日期。
LW:表示某个月最后一个工作日。
#:用于确定每个月第几个星期几,只能出现在第六位(星期)域,例如4#3,表示某月的第3个星期三。
C::只能用在第四位(日)和第六位(星期)两个域,需要关联日历,如果没关联可以忽略。
0 0 2 1 * ? * 表示在每月的1日的凌晨2点执行
0 15 10 ? * 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点执行
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时执行
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
注意
专有符号中除?外,在 spirng 定时任务中都不支持.
测试
@Scheduled(cron = "* * 20 ? * 1#3")
public void configureTasks1() {
System.err.println("执行定时任务: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
项目启动的时候就会出现异常
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'configureTasks1': For input string: "1#3"
那在哪里能使用呢??Quartz支持。
在线Cron表达式生成器可以去测试一下.
异步多线程实现
定时任务默认是单线程的,如果某个任务持续时间较长,就会将后面的定时任务拖延,导致丢失任务。
测试
@Scheduled(cron = "0/1 * * * * ?")
public void configureTasks() throws InterruptedException {
System.out.println("线程名111:" + Thread.currentThread().getName() +"===" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
Thread.sleep(3000);
System.err.println("执行定时任务111: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
@Scheduled(cron = "0/1 * * * * ?")
public void configureTasks2() throws InterruptedException {
System.out.println("线程名2222:" + Thread.currentThread().getName() +"===" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
System.err.println("执行222: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
可以看出这两个定时任务都是同一个线程在执行,如果有一个定时任务执行比较耗时(比如:configureTasks模拟耗时,开启线程睡眠),就会造成任务丢失。
开启异步注解
启动类上加上注解
@EnableAsync
设置异步执行
@Scheduled(cron = "0/1 * * * * ?")
@Async
public void configureTasks() throws InterruptedException {
System.out.println("线程名111:" + Thread.currentThread().getName() + "===="+ SimpleDateFormat.getDateTimeInstance().format(new Date()));
Thread.sleep(2000);
}
@Scheduled(cron = "0/1 * * * * ?")
@Async
public void configureTasks2() throws InterruptedException {
System.out.println("线程名2222:" + Thread.currentThread().getName() + "====" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
//Thread.sleep(1000);
}
从结果可以看出,configureTasks2方法已经不会因为configureTasks方法处理缓慢而被影响了。而且每个定时任务都是由不同的线程执行。task-xxx(默认)就是线程名。在实际开发中,我们都会自定义异步线程池,合理管理线程数量,并定义线程名称,方便问题排查。
创建AsyncConfig配置类
@Configuration
public class AsyncConfig {
//最大线程数
private static final int MAX_POOL_SIZE = 5;
//核心线程数
private static final int CORE_POOL_SIZE = 2;
//队列容量
private static final int QUEUE_CAPACITY = 3;
@Bean("testTaskExecutor")
public AsyncTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
//活跃时间
taskExecutor.setKeepAliveSeconds(60);
//线程名字前缀
taskExecutor.setThreadNamePrefix("test-async-task-thread-pool-");
return taskExecutor;
}
}
修改CronController类的方法
@Scheduled(cron = "0/1 * * * * ?")
@Async("testTaskExecutor")
public void configureTasks() throws InterruptedException {
System.out.println("线程名111:" + Thread.currentThread().getName() +"===" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
Thread.sleep(3000);
System.err.println("执行定时任务111: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
@Scheduled(cron = "0/1 * * * * ?")
@Async("testTaskExecutor")
public void configureTasks2() throws InterruptedException {
System.out.println("线程名2222:" + Thread.currentThread().getName() +"===" + SimpleDateFormat.getDateTimeInstance().format(new Date()));
System.err.println("执行222: " + SimpleDateFormat.getDateTimeInstance().format(new Date()));
}
@Async("testTaskExecutor")指定线程池名称。
- 如你对本文有疑问或本文有错误之处,欢迎评论留言指出。如觉得本文对你有所帮助,欢迎点赞和关注。