ServletContextListener 實現類的contextDestroyed ()方法不執行

public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("===========關閉spider!===========");
        stopSpider();       
 }

ServletContextListener 接口的contextDestroyed () 方法用於工程停止時釋放資源,但使用eclipse集成的tomcat進行測試時,資源沒有釋放。

爲了判斷方法是否執行,我在方法裏面添加了System.out,但控制檯沒有打印出“========關閉spider!=========”。

查資料發現通過eclipse關閉tomcat的時候,如果在console界面關閉,實際上是Terminate,而在Servers界面關閉是Stop,Stop可以正常在控制檯打印出銷燬信息,推測Terminate可能是直接關閉tomcat進程,所以還沒有調用contextDestroyed()方法就結束了,所以看不到銷燬信息。

之後我將工程部署到服務器測試,服務器停止時資源正常釋放了,看日誌有“========關閉spider!=========”輸出;說明部署到服務之後,關閉服務器時contextDestroyed()方法會正常調用。

參考:https://blog.csdn.net/FreemanZhao/article/details/54918130?utm_source=blogxgwz1

           https://blog.csdn.net/tempdowncomputer/article/details/84410989

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