原文地址:https://www.cnblogs.com/alalazy/p/12764742.html
一、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");
}
}
二、配置多个定时任务并发执行
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
网友评论