Netty 应用 | 跟踪诊断 | Console | JMX
    https://blog.csdn.net/weixin_33669968/article/details/104552393

    1. @ChannelHandler.Sharable
    2. public class MetricHandler extends ChannelDuplexHandler {
    3. private AtomicLong totalConnectionNumber = new AtomicLong();
    4. {
    5. MetricRegistry metricRegistry = new MetricRegistry();
    6. metricRegistry.register("totalConnectionNumber", new Gauge<Long>() {
    7. public Long getValue() {
    8. return totalConnectionNumber.longValue();
    9. }
    10. });
    11. ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();
    12. consoleReporter.start(5, TimeUnit.SECONDS);
    13. JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
    14. jmxReporter.start();
    15. }
    16. @Override
    17. public void channelActive(ChannelHandlerContext ctx) throws Exception {
    18. totalConnectionNumber.incrementAndGet();
    19. super.channelActive(ctx);
    20. }
    21. @Override
    22. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    23. totalConnectionNumber.decrementAndGet();
    24. super.channelInactive(ctx);
    25. }
    26. }