tomcat參數引發後臺建立影子任務


所謂後臺影子任務

是在程序設置的自動&定時任務運行過程當中同一個任務啓動了兩次,對,兩次,不多不少,就在那裏。

第一次任務開始後第二次任務(不該存在的)時隔差不多30s-50s便開始執行。


Spring註解關鍵字Scheduled定時任務

代碼:@Scheduled(fixedDelay = 1000 * 60 * 10)

任務是每隔10分鐘執行一次。

問題已經解決了,當時看數據情況是這樣的,用excel模擬一下任務日誌表。





不難看出,任務ID 1、3、5 是每個10分鐘執行一次的任務,可是任務ID  2、4、6是什麼鬼?

 

難道是服務器tomcat老進程沒關掉,導致任務線程重複?

可是每次都kill掉tomcat了,並且重啓多次tomcat,進程沒殺掉也應該很多任務纔對,爲什麼人家還是這麼“成雙成對”?

 

其實任務線程重複這個思路沒有問題,

Google一下:“Quartz job runs twice when deployed on tomcat ”


 tomcat配置中有此兩個參數引起注意:

 

大體意思是參數默認爲true,deploy發佈則自動啓動。

Stackflow中老外的解釋是:

Whenusing automatic deployment, the docBase defined by an XML Context file shouldbe outside of the appBase directory. If this is not the case, difficulties maybe experienced deploying the web application or the application may be deployedtwice. The deployIgnore attribute can be used to avoid this situation.

Finally, note that if you are defining contexts explicitly inserver.xml, you should probably turn off automatic application deployment orspecify deployIgnore carefully. Otherwise, the web applications will each bedeployed twice, and that may cause problems for the applications.


deploy自啓動一次+startup啓動一次,果然是tomcat配置默認參數導致的重複任務。

 

解決方法

找到tomcat  server.xml文件,修改如下:

Host name="localhost" 
        deployOnStartup="false"  

        appBase="webapps" 
        unpackWARs="false" 




重新啓動tomcat,任務日誌表如下:


一切都是那麼自然……


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章