2015-09-21 43 views
1

我正在使用Netty 4.0.26.Final和Tomcat 8一起在Web應用程序內實現嵌入式TCP服務器。在tomcat中導致內存泄露的Netty

的問題是,當我停止Tomcat我得到這個消息:

The web application [app] appears to have started a thread named 

[nioEventLoopGroup-2-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: 

sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method) 
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296) 
sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278) 
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159) 
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87) 
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98) 
io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622) 
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310) 
io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) 
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137) 

我已經使用這個類關機關閉Tomcat的前線程嘗試,但我仍然有同樣的問題。

@Component 
public class MyAppServletContextListener implements ServletContextListener{ 

    @Override 
    public void contextInitialized(ServletContextEvent arg0) { 
    } 


    @Override 
    public void contextDestroyed(ServletContextEvent arg0) { 
     TrackingDaemon trackingDaemon= (TrackingDaemon) ApplicationContextProvider.getBean("trackingDaemon"); 
     trackingDaemon.bossGroup.shutdownGracefully(); 
     trackingDaemon.workerGroup.shutdownGracefully(); 
    } 


} 

BossGroup和workerGroup是:

EventLoopGroup bossGroup = new NioEventLoopGroup(); 
EventLoopGroup workerGroup = new NioEventLoopGroup(); 

任何幫助將不勝感激..

回答

1

我有這個問題使用netty 4和tomcat 7,並通過從shutdownGracefully()中捕獲未來事件並等待其完成來解決此問題上。 關閉應用程序時會調用以下方法。

private void stopChannel() { 
log.trace("Stopping server socket on port '{}'", port); 
sendMessageService.deregisterMessageSender(endpointId); 
try { 
    for (Channel channel : channels) { 
    if (channel != null) { 
     channel.close(); 
    } 
    } 
    serverChannel.close().sync(); 
    log.trace("Server socket on port '{}' stopped.", port); 
} catch (InterruptedException e) { 
    throw new RuntimeException(e); 
} finally { 
    Future<?> fc = connectionGroup.shutdownGracefully(); 
    Future<?> fw = workerGroup.shutdownGracefully(); 
    try { 
    fc.await(); // when shutting down in tomcat waits for the netty threads to die 
    fw.await(); 
    } catch (InterruptedException e) { 
    throw new RuntimeException(e); 
    } 
    log.trace("Server workerGroup has shutdown successfully on port '{}'", port); 
} 

}

0

我想你需要做:

trackingDaemon.bossGroup.shutdownGracefully().syncUninterruptibly(); 
    trackingDaemon.workerGroup.shutdownGracefully().syncUninterruptibly();