Quartz關閉Tomcat時異常:The web application [/****] appears to have started a thread named [startQuertz_Worker-1] buthas


原文地址:http://blog.csdn.net/huilixiang/article/details/8730520

 

在tomcat7+quartz1.8/1.7 + spring3.0.5做定時任務的時候 , 當關閉tomcat時會發現如下異常:

ar 27, 2013 6:05:35 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-nio-8082"]
Mar 27, 2013 6:05:35 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Mar 27, 2013 6:05:35 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/****] appears to have started a thread named [startQuertz_Worker-1] buthas       failed to stop it. This is very likely to create a memory leak.
Mar 27, 2013 6:05:35 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/****] appears to have started a thread named [startQuertz_Worker-2] buthas  failed to stop it. This is very likely to create a memory leak.
Mar 27, 2013 6:05:35 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/****] appears to have started a thread named [startQuertz_Worker-3] buthas   failed to stop it. This is very likely to create a memory leak.
Mar 27, 2013 6:05:35 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

 

首先懷疑線程泄漏 , 依各位前輩的經驗建議 , 修改quartz的配置:

 

    <bean id="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>                
                
            </list>
        </property>
        <property name="quartzProperties">  
        <props>  
            <prop key="org.quartz.scheduler.instanceName">buy_it_now</prop>
            <prop key="org.quartz.threadPool.threadCount">2</prop>  
            <prop key="org.quartz.plugin.shutdownhook.class">org.quartz.plugins.management.ShutdownHookPlugin</prop>
            <prop key="org.quartz.plugin.shutdownhook.cleanShutdown">true</prop>
            <prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop>
        </props>  
    </property>  

 

未果 , 以上配置只是設置了線程池的大小和在shutdown時進行清理 , 結果在老外的網站上發現了解決辦法。

日志warn可能線程泄漏 ,但事實並非如此(本人沒考證) , 只是tomcat在shutdown做清理工作的時候沒能等待quartz完成cleanShutdown !就是tomcat太心急了 , 說 “quartz , 我關門了 , 你走吧!” , 還沒等quartz反應過來,

就要關大門 , 這時發現 “quartz , 你怎么還在這兒呀!”。 解決的辦法簡單:自己實現一個ServletContextListener ,

在contextDestroyed的時候主動調用quartz schedular的shutdown方法 ,並且主線程sleep一會兒 , 上例子:

public class QuartzContextListener implements ServletContextListener {

    /*
     * 測試代碼寫得隨便
     * 
     * @seejavax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
     * ServletContextEvent)
     */
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        WebApplicationContext webApplicationContext = (WebApplicationContext) arg0
                .getServletContext()
                .getAttribute(
                        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        org.quartz.impl.StdScheduler startQuertz = (org.quartz.impl.StdScheduler) webApplicationContext
                .getBean("startQuertz");
        if(startQuertz != null) {
            startQuertz.shutdown();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
     * .ServletContextEvent)
     */
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
<span style="white-space:pre">        </span>//不做任何事情
    }

}

 

別忘了web.xml中添加起! 結束

我所遇到的問題和他的一樣,解決的辦法就是在實現了ServletContextListener的類中加入了讓線程停止一會的代碼:

public void contextDestroyed(ServletContextEvent sce) {
        if (jobSchedule != null) {
            jobSchedule.stopScheduler();
        }
        
        try {  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM