Netty 应用 | 跟踪诊断 | Console | JMX
https://blog.csdn.net/weixin_33669968/article/details/104552393
@ChannelHandler.Sharable
public class MetricHandler extends ChannelDuplexHandler {
private AtomicLong totalConnectionNumber = new AtomicLong();
{
MetricRegistry metricRegistry = new MetricRegistry();
metricRegistry.register("totalConnectionNumber", new Gauge<Long>() {
public Long getValue() {
return totalConnectionNumber.longValue();
}
});
ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();
consoleReporter.start(5, TimeUnit.SECONDS);
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
jmxReporter.start();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
totalConnectionNumber.incrementAndGet();
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
totalConnectionNumber.decrementAndGet();
super.channelInactive(ctx);
}
}