SpringBoot配置及使用Schedule过程解析
我们在平常项目开发中,经常会用到周期性定时任务,这个时候使用定时任务就能很方便的实现。在springboot中用得最多的就是schedule。
一、springboot集成schedule
1、依赖配置
由于schedule就包含在spring-boot-starter中,所以无需引入其他依赖。
2、启用定时任务
在启动类或者配置类上增加@enablescheduling注解。
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.scheduling.annotation.enablescheduling; @enablescheduling @springbootapplication public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); } }
3、添加定时任务
schdule支持cron表达式、固定间隔时间、固定频率三种调度方式。
1)cron表达式定时任务
与linux下定时任务用到的cron表达式一样。
字段 | 允许值 | 允许的特殊字符 |
秒(seconds) | 0~59的整数 | , - * / 四个字符 |
分(minutes) | 0~59的整数 | , - * / 四个字符 |
小时(hours) | 0~23的整数 | , - * / 四个字符 |
日期(dayofmonth) | 1~31的整数(但是你需要考虑该月的天数) | ,- * ? / l w c 八个字符 |
月份(month) | 1~12的整数或者 jan-dec | , - * / 四个字符 |
星期(dayofweek) | 1~7的整数或者 sun-sat (1=sun) | , - * ? / l c # 八个字符 |
年(可选,留空)(year) | 1970~2099 | , - * / 四个字符 |
@component @enablescheduling public class mycrontask { private static final logger logger = loggerfactory.getlogger(mycrontask.class); @scheduled(cron = "0/1 * * * * *") void cronschedule(){ logger.info("cron schedule execute"); } }
ps:cron表达式方式配置的定时任务如果其执行时间超过调度频率时,调度器会在下个执行周期执行。如第一次执行从第0秒开始,执行时长3秒,则下次执行为第4秒。
2)固定间隔定时任务
下一次的任务执行时间是从上一次定时任务结束时间开始计算。
@scheduled(fixeddelay = 2) void fixeddelayschedule() throws exception{ thread.sleep(2000); logger.info("fixed delay schedule execute"); }
输出:
2020-04-23 23:11:54.362 info 85325 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed delay schedule execute
2020-04-23 23:11:58.365 info 85325 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed delay schedule execute
2020-04-23 23:12:02.372 info 85325 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed delay schedule execute
2020-04-23 23:12:06.381 info 85325 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed delay schedule execute
3)固定频率定时任务
按照指定频率执行任务
@scheduled(fixedrate = 2000) void fixedrateschedule() throws exception{ thread.sleep(3000); logger.info("fixed rate schedule execute"); }
输出:
2020-04-23 23:16:14.750 info 85328 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed rate schedule execute
2020-04-23 23:16:17.754 info 85328 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed rate schedule execute
2020-04-23 23:16:20.760 info 85328 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed rate schedule execute
2020-04-23 23:16:23.760 info 85328 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed rate schedule execute
2020-04-23 23:16:26.764 info 85328 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : fixed rate schedule execute
ps:当方法的执行时间超过任务调度频率时,调度器会在当前方法执行完成后立即执行下次任务。
二、配置多个定时任务并发执行
1、并行or串行?
缺省状态下,当我们没有给定时任务配置线程池时,schedule是串行执行,如下:
@component @enablescheduling public class mycrontask { private static final logger logger = loggerfactory.getlogger(mycrontask.class); @scheduled(fixeddelay = 2000) void task1schedule() throws exception{ thread.sleep(2000); logger.info("task1 execute"); } @scheduled(fixeddelay = 2000) void task2schedule() throws exception{ thread.sleep(2000); logger.info("task2 execute"); } @scheduled(fixeddelay = 2000) void task3schedule() throws exception{ thread.sleep(2000); logger.info("task3 execute"); } }
输出:
2020-04-23 23:19:46.970 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:19:48.973 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:19:50.974 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:19:52.978 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:19:54.984 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:19:56.984 info 85332 --- [ scheduling-1] com.springboot.study.tasks.mycrontask : task3 execute
可以看出来只有一个线程穿行执行所有定时任务。
2、schedule并行执行配置
定时调度的并行化,有两种配置方式:
1)修改任务调度器默认使用的线程池:添加一个configuration,实现schedulingconfigurer接口就可以了。
@configuration public class scheduleconfig implements schedulingconfigurer{ @override public void configuretasks(scheduledtaskregistrar taskregistrar) { taskregistrar.settaskscheduler(gettaskscheduler()); } @bean public taskscheduler gettaskscheduler() { threadpooltaskscheduler taskscheduler = new threadpooltaskscheduler(); taskscheduler.setpoolsize(3); taskscheduler.setthreadnameprefix("myworker-"); taskscheduler.setwaitfortaskstocompleteonshutdown(true); return taskscheduler; } }
再次执行后,输出:
2020-04-23 23:33:14.197 info 85461 --- [ myworker-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:33:14.197 info 85461 --- [ myworker-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:33:14.197 info 85461 --- [ myworker-3] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:33:18.203 info 85461 --- [ myworker-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:33:18.203 info 85461 --- [ myworker-3] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:33:18.204 info 85461 --- [ myworker-1] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:33:22.208 info 85461 --- [ myworker-1] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:33:22.208 info 85461 --- [ myworker-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:33:22.208 info 85461 --- [ myworker-3] com.springboot.study.tasks.mycrontask : task1 execute
2)直接将任务交给一步线程池处理:启用@enableasync注解,并在每一个定时任务方法上使用@async注解。
@component @enablescheduling @enableasync @async public class mycrontask { private static final logger logger = loggerfactory.getlogger(mycrontask.class); @scheduled(fixeddelay = 2000) void task1schedule() throws exception{ thread.sleep(2000); logger.info("task1 execute"); } @scheduled(fixeddelay = 2000) void task2schedule() throws exception{ thread.sleep(2000); logger.info("task2 execute"); } @scheduled(fixeddelay = 2000) void task3schedule() throws exception{ thread.sleep(2000); logger.info("task3 execute"); } }
输出如下:
2020-04-23 23:38:00.614 info 85468 --- [ task-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:38:00.614 info 85468 --- [ task-3] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:38:00.614 info 85468 --- [ task-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:38:02.620 info 85468 --- [ task-4] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:38:02.620 info 85468 --- [ task-5] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:38:02.620 info 85468 --- [ task-6] com.springboot.study.tasks.mycrontask : task3 execute
有上面输出可以看出来这种方式对于每一次定时任务的执行都会创建新的线程,这样对内存资源是一种浪费,严重情况下还会导致服务挂掉,因此为了更好控制线程的使用,我们可以自定义线程池。
首先配置线程池:
@configuration public class mytaskexecutor { @bean(name = "myexecutor") public taskexecutor getmyexecutor() { threadpooltaskexecutor taskexecutor = new threadpooltaskexecutor(); taskexecutor.setcorepoolsize(3); taskexecutor.setmaxpoolsize(10); taskexecutor.setqueuecapacity(20); taskexecutor.setthreadnameprefix("myexecutor-"); taskexecutor.initialize(); return taskexecutor; } }
使用我们自己的线程池:
@component @enablescheduling @enableasync @async("myexecutor") public class mycrontask { private static final logger logger = loggerfactory.getlogger(mycrontask.class); @scheduled(fixeddelay = 2000) void task1schedule() throws exception{ thread.sleep(2000); logger.info("task1 execute"); } @scheduled(fixeddelay = 2000) void task2schedule() throws exception{ thread.sleep(2000); logger.info("task2 execute"); } @scheduled(fixeddelay = 2000) void task3schedule() throws exception{ thread.sleep(2000); logger.info("task3 execute"); } }
输出:
2020-04-23 23:46:47.404 info 85488 --- [ myexecutor-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:46:47.404 info 85488 --- [ myexecutor-3] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:46:47.404 info 85488 --- [ myexecutor-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:46:49.404 info 85488 --- [ myexecutor-3] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:46:49.404 info 85488 --- [ myexecutor-2] com.springboot.study.tasks.mycrontask : task3 execute
2020-04-23 23:46:49.404 info 85488 --- [ myexecutor-1] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:46:51.405 info 85488 --- [ myexecutor-2] com.springboot.study.tasks.mycrontask : task2 execute
2020-04-23 23:46:51.405 info 85488 --- [ myexecutor-3] com.springboot.study.tasks.mycrontask : task1 execute
2020-04-23 23:46:51.405 info 85488 --- [ myexecutor-1] com.springboot.study.tasks.mycrontask : task3 execute
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 使用鼠标时明显感觉到停顿怎么办?
推荐阅读