java - ResourceLeakDetector.setLevel(PARANOID) 不产生资源泄漏日志
问题描述
我有一个 netty 4 http 服务器已实现。
public void start() {
System.out.println("In Start method");
try {
ServerBootstrap b = new ServerBootstrap();
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new HttpServerPipelineFactory())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
.childOption(ChannelOption.AUTO_READ, false)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(listenerPort).sync();
System.out.println("server started listening on port " + listenerPort);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
}
HTTP 管道工厂类是 -
public class HttpServerPipelineFactory extends ChannelInitializer<Channel> {
@Override
protected void initChannel(Channel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("codec", new HttpServerCodec());
pipeline.addLast("compress", new HttpContentCompressor());
pipeline.addLast("decompress", new HttpContentDecompressor());
pipeline.addLast("aggregator", new HttpObjectAggregator( 512 * 1024));
pipeline.addLast("chunked", new ChunkedWriteHandler());
pipeline.addLast("flow", new FlowControlHandler());
pipeline.addLast("keep-alive", new HttpServerKeepAliveHandler());
pipeline.addLast("request", new AdvancedHTTPHandler());
}
}
AdvancedHTTPHandler 是 -
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static io.netty.util.CharsetUtil.UTF_8;
public class AdvancedHTTPHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private static Logger logger = LoggerFactory.getLogger(HTTPRequestHandler.class);
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer("My Netty".getBytes()), false);
response.headers().add(request.headers());
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
ctx.channel().writeAndFlush(response);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.read();
}
}
在 Main.java -
public class Main {
public static void main(String[] args) {
ResourceLeakDetector.setLevel(PARANOID);
HttpServer server = new HttpServer(5020);
server.start();
}
}
正如您在主应用程序中看到的那样,我已经设置ResourceLeakDetector.setLevel(PARANOID);
但在我的应用程序日志中没有看到任何资源泄漏检测日志。
我错过了什么。我该如何解决这个问题?
解决方案
我在您的代码中看不到任何泄漏,因此没有报告泄漏是有道理的。只需替换SimpleChannelInboundHandler
或ChannelInboundHandlerAdaptor
调用request.retain()
您的channelRead0(...)
方法,您就会看到一个报告。
推荐阅读
- elasticsearch - _count 在弹性搜索中的作用是什么?
- powershell - 如何单击复选框管理器可以使用 PowerShell 更新 Active Directory 组上的成员列表
- javascript - 如何从数组中找到数据?
- node.js - 如何在 Windows 10 上修复电子 sqlite3 重建错误(语法错误)
- amazon-web-services - 使用 ansible 创建 aws 目标组
- math - 如何计算两个圆圈外部接触的时间?
- javascript - 如何使用javascript在html页面中将英文数字显示为bangla unicode格式?
- javascript - 在 React Native 上无法使用 : 读取文件名
- javascript - HTML:如何在包装标签中引用特定的 URL
- javascript - 在 Angular 中的 POST 请求后调用 GET 请求