samsungsamsung 发表于 2017-2-4 12:42:10

tomcat 中关于自定义线程的关闭

开发环境:
Tomcat7 + Spring3 + Hibernate3
现象:
在tomcat中自定义的线程不能随着应用的停止而关闭(可以随着tomcat的停止而关闭)。
伴随异常:
-----------
...
严重:The web application ... appears to have started a thread named but has failed to stop it. This is very likely to createa memory leak.
...
解决方法:
在停止应用的时候应该加入关闭自定义线程的处理。
以下是使用mina的例子:
1.应用开始的时候通过xml中的Listener初始化一个acceptor(acceptor的具体设定不在列出)
2.应用停止的时候在contextDestroyed中对acceptor其进行关闭。
web.xml

...
<listener>
<listener-class>com.mina.test.InitListener</listener-class>
</listener>
...


InitListener.java

public class InitListener implements ServletContextListener
...
private ApplicationContext context;
public void contextDestroyed(ServletContextEvent arg0) {
// 应用关闭的时候调用NioSocketAcceptor的dispose()方法
// ioAcceptor就是定义在applicationContext-minaServer.xml中的bean名字
NioSocketAcceptor nioacceptor = (NioSocketAcceptor)context.getBean("ioAcceptor");
nioacceptor.dispose();
}
public contextInitiialized(ServletContextEvent arg0) {
// 应用开始的时候初始化mina,会创建一个NioSocketAcceptor进程
context = new ClassPathXmlApplicationContext("applicationContext-minaServer.xml");
}
页: [1]
查看完整版本: tomcat 中关于自定义线程的关闭