最近在是使用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"> ...
產生調度日志如下(完美解決)。