严重: The web application [] appears to have started a thread named [Thread-

来源:互联网 发布:js动态添加a标签 编辑:程序博客网 时间:2024/04/30 12:21

严重: The web application [] appears to have started a thread named [Thread-

www.MyException.Cn  网友分享于:2015-02-04   搜索量:2572次
<iframe id="iframeu1107459_0" src="http://pos.baidu.com/iccm?rdid=1107459&amp;dc=2&amp;di=u1107459&amp;dri=0&amp;dis=0&amp;dai=3&amp;ps=263x502&amp;dcb=BAIDU_SSP_define&amp;dtm=BAIDU_DUP_SETJSONADSLOT&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1466406740139&amp;ti=%E4%B8%A5%E9%87%8D%3A%20The%20web%20application%20%5B%5D%20appears%20to%20have%20started%20a%20thread%20&amp;ari=1&amp;dbv=2&amp;drs=1&amp;pcs=1293x551&amp;pss=1293x527&amp;cfv=0&amp;cpl=4&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1430680323&amp;ltu=http%3A%2F%2Fwww.myexception.cn%2Fh%2F988782.html&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DVuJENSw-xov8qLLOzuDBjOeD969YRPV-vlhqU12WxJd-vj_W_1TbuhFDFgaOcHmNCpELIdx2eW0374AUsQziEq%26wd%3D%26eqid%3Db404a01e00008c900000000357679853&amp;ecd=1&amp;psr=1366x768&amp;par=1366x728&amp;pis=-1x-1&amp;ccd=24&amp;cja=true&amp;cmi=6&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1466406740&amp;qn=80377c0e5e90c4b2&amp;tt=1466406740119.331.427.433" width="300" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; vertical-align: bottom;"></iframe>

1,获取dump


2,从dump找到正在运行的线程所属class


3,在myeclise 开启debug模式,给正在运行的方法加上断点


4,这时你会发现当停止application时,有一个线程还没有停止掉


5,在 AppContextListener 进行contextDestroyed时,要加一段额外的代码,用来我们关闭tomcat的时候可以同时关闭此线程



例如此例:


在tomcat7+quartz1.8/1.7 + spring3.0.5做定时任务的时候 , 当关闭tomcat时抛异常"严重: The web application [] appears to have started a thread named [Thread-":


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

原因: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 配置QuartzContextListener。



注意:如果tomcat在Dameon模式下,以上方法不起作用,请参考:
http://wiki.apache.org/tomcat/MemoryLeakProtection


0 1