1、spring+quartz关闭Tomcat出现异常
生活随笔
收集整理的這篇文章主要介紹了
1、spring+quartz关闭Tomcat出现异常
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
quartz + spring做定時任務的時候 , tomcat8重啟/關閉服務出現如下異常:
appears to have started a thread named [startQuertz_xxx] but has failed to stop it.原因:
tomcat在shutdown做清理工作的時候沒能等待quartz完成cleanShutdown工作時就已經關閉 !
解決方案:自己實現ServletContextListener
package com.wxmp.task;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;import org.quartz.Scheduler; import org.springframework.web.context.support.WebApplicationContextUtils;/*** @author Lee* @date 2018年6月25日* @function 沒等quartz clean關閉時,* tomcat已經關閉了,導致quartz線程一直占用著8080/jxm port:1099端口*/ public class QuartzContextListener implements ServletContextListener {@Override public void contextDestroyed(ServletContextEvent event) { try{ Scheduler startQuartz = (Scheduler) WebApplicationContextUtils .getWebApplicationContext(event.getServletContext()) .getBean("myScheduler"); if(startQuartz != null){ startQuartz.shutdown(true); } Thread.sleep(1000);//主線程睡眠1s }catch (Exception e){ e.printStackTrace(); } event.getServletContext().log("QuartzContextListener銷毀成功!"); System.out.println("QuartzContextListener銷毀成功!"); } @Override public void contextInitialized(ServletContextEvent event) { System.out.println("QuartzContextListener啟動成功!"); event.getServletContext().log("QuartzContextListener啟動成功!"); } }web.xml
<!-- quartz關閉 --> <listener><listener-class>com.wxmp.task.QuartzContextListener</listener-class> </listener>?
轉載于:https://my.oschina.net/u/3551274/blog/1835636
總結
以上是生活随笔為你收集整理的1、spring+quartz关闭Tomcat出现异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java爬虫模拟登录——不给我毛概二的H
- 下一篇: logstash配置文件