媒介:在运用Springboot整合定时使命,发明当某个定时使命实行涌现实行时候太长的状况时会壅塞其他定时使命的实行。
题目定位
后续经由过程翻查Springboot的文档以及打印日记(输出当前线程信息)得知题目是由于Springboot默许运用只需1个线程处置惩罚定时使命。
题目复盘
须要注重示例的Springboot版本为2.1.3.RELEASE。
症结pom文件设置
<!--继续父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> ...省略非症结设置 <!-- 引入依靠--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
定时使命
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 定时使命 * @author RJH * create at 2019-03-29 */ @Component public class SimpleTask { private static Logger logger= LoggerFactory.getLogger(SimpleTask.class); /** * 实行会超时的使命,定时使命距离为5000ms(等价于5s) */ @Scheduled(fixedRate = 5000) public void overtimeTask(){ try { logger.info("current run by overtimeTask"); //休眠时候为实行距离的2倍 Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 一般的定时使命 */ @Scheduled(fixedRate = 5000) public void simpleTask(){ logger.info("current run by simpleTask"); } }
启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class TaskDemoApplication { public static void main(String[] args) { SpringApplication.run(TaskDemoApplication.class, args); } }
运转效果
...省略非症结信息 2019-03-29 21:22:38.410 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by simpleTask 2019-03-29 21:22:38.413 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by overtimeTask 2019-03-29 21:22:48.413 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by simpleTask 2019-03-29 21:22:48.414 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by overtimeTask 2019-03-29 21:22:58.418 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by simpleTask 2019-03-29 21:22:58.418 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by overtimeTask 2019-03-29 21:23:08.424 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by simpleTask 2019-03-29 21:23:08.424 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by overtimeTask 2019-03-29 21:23:18.425 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by simpleTask 2019-03-29 21:23:18.426 INFO 59731 --- [ scheduling-1] com.rjh.task.SimpleTask : current run by overtimeTask ...
效果剖析
由运转效果能够看出:
- 每次定时使命的运转都是由
scheduling-1
这个线程处置惩罚 - 一般运转的
simpleTask
被overtimeTask
壅塞致使了运转距离变成了10
秒
背面经由过程查阅Springboot
的文档也得知了定时使命默许最大运转线程数为1
。
处理方案
由于运用的Springboot
版本为2.1.3.RELEASE
,所以有两种要领处理这个题目
运用Springboot设置
在设置文件中能够设置定时使命可用的线程数:
## 设置可用线程数为10 spring.task.scheduling.pool.size=10
自定义定时使命的线程池
运用自定义的线程池替代默许的线程池
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /** * 定时使命设置类 * @author RJH * create at 2019-03-29 */ @Configuration public class ScheduleConfig { /** * 此处要领名为Bean的名字,要领名无需牢固 * 由于是按TaskScheduler接口自动注入 * @return */ @Bean public TaskScheduler taskScheduler(){ // Spring供应的定时使命线程池类 ThreadPoolTaskScheduler taskScheduler=new ThreadPoolTaskScheduler(); //设定最大可用的线程数量 taskScheduler.setPoolSize(10); return taskScheduler; } }
本篇文章到这里就已悉数完毕了,更多其他精彩内容能够关注ki4网的Java视频教程栏目!
以上就是Springboot定时使命碰到的题目处理的细致内容,更多请关注ki4网别的相干文章!