gpt4 book ai didi

java - Netty 导致 tomcat 内存泄漏

转载 作者:行者123 更新时间:2023-11-28 22:18:35 28 4
gpt4 key购买 nike

我正在使用 Netty 4.0.26.Final 和 Tomcat 8 在 Web 应用程序中实现嵌入式 TCP 服务器。

问题是,当我停止 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();

任何帮助将不胜感激..

最佳答案

我在 netty 4 和 tomcat 7 上遇到过这个问题,并通过从 shutdownGracefully() 捕获 future 事件并等待其完成来解决它。关闭应用程序时调用以下方法。

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);
}

关于java - Netty 导致 tomcat 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32696658/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com