您的位置:首页 > 编程语言 > Java开发

Spring定时任务有时候会莫名奇妙的终止?

2017-11-30 16:46 253 查看
最近在是使用Spring配置定时定时任务(基于xml配置使用spring自带的定时任务),一开始使用没什么问题当使用久了就会出现有些定时任务自动停止了。(关于如何使用以及如何它的原理是啥,这里不进行阐述)

配置案例如下:

<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">
...


产生调度日志如下(完美解决)。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: