Spring定时任务有时候会莫名奇妙的终止?
2017-11-30 16:46
253 查看
最近在是使用Spring配置定时定时任务(基于xml配置使用spring自带的定时任务),一开始使用没什么问题当使用久了就会出现有些定时任务自动停止了。(关于如何使用以及如何它的原理是啥,这里不进行阐述)
配置案例如下:
定时任务调度如下,从图中可以看出spring定时任务只开启一个线程去工作也就是串行工作。在实际项目中,其中collectException定时任务会无故终止且日志中也没有打印错误。当中也排查了内存不足的问题,后面仔细排查发现有定时调度任务出现阻塞导致线程终止。
解决方法:配置线程池并配置具体线程数(根据自己有多少个定时调度任务会同时执行的情况下考虑设置)使得定时调度任务能并行执行且不会阻塞。配置如下(后面省略):
产生调度日志如下(完美解决)。
配置案例如下:
<task:scheduled-tasks scheduler="scheduler"> <task:scheduled ref="deleteMoniterTimer" method="delMoniterByHost" cron="0 0/5 * * * ?" /> <task:scheduled ref="deleteAlarmAndLogTimer" method="deleteAlarmAndLog" cron="0 0 1 * * ?" /> <task:scheduled ref="uapHeartBeatTimer" method="heartBeat" cron="0 */1 * * * ?" /> <task:scheduled ref="uapUserChangeTimer" method="uapUserCheck" cron="0 0 * * * ?" /> <task:scheduled ref="singleLoginClearTimer" method="singleLoginClear" cron="0 0 0 * * ?" /> <task:scheduled ref="alarmJmsTimer" method="publishAlarmInfo" cron="*/5 * * * * ?" /> <task:scheduled ref="deleteHistoryAnalysisTimer" method="deleteInvalidRecord" cron="0 0 0 * * ?" /> <task:scheduled ref="collectExceptionAlarmTimer" method="judgeCollectException" cron="0 */1 * * * ?" /> </task:scheduled-tasks>
定时任务调度如下,从图中可以看出spring定时任务只开启一个线程去工作也就是串行工作。在实际项目中,其中collectException定时任务会无故终止且日志中也没有打印错误。当中也排查了内存不足的问题,后面仔细排查发现有定时调度任务出现阻塞导致线程终止。
解决方法:配置线程池并配置具体线程数(根据自己有多少个定时调度任务会同时执行的情况下考虑设置)使得定时调度任务能并行执行且不会阻塞。配置如下(后面省略):
<!-- 配置线程池并设置线程数的初始大小--> <task:scheduler id="scheduler" pool-size="20" /> <task:scheduled-tasks scheduler="scheduler"> ...
产生调度日志如下(完美解决)。
相关文章推荐
- Java Spring Quartz 定时任务
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring教程_Spring定时任务_@Scheduled使用
- spring整合Quartz定时器(二)---基于@Scheduled注解执行定时任务
- spring4 mvc 定时任务
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- Spring定时任务处理 conTrigger 定时器表达式大全
- Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查
- Spring整合Quartz实现定时任务调度的方法
- 10-Spring Boot ( 定时任务调度 )
- spring 使用注解来调度定时任务
- java spring 定时任务
- Spring定时任务的几种实现
- Spring整合TimerTask实现定时任务调度
- 使用spring 的Quartz 定时任务入门
- Spring定时任务的几种实现
- Spring定时任务开发,实现动态修改时间参数,手动开启关停等
- spring定时任务.线程池,自定义多线程配置
- spring Quartz定时任务调度 时间设置
- Spring+Quartz实现定时任务的配置方法