如何监控Netty EventLoopGroup线程池

How to monitor Netty EventLoopGroup threadpool

我有一个用 Netty 构建的服务器,它的线程池基于 bossGroup/workerGroup 模型。这是基本的 Netty 服务器实现:

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


    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)                 
        .channel(NioServerSocketChannel.class)
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new TelnetServerInitializer());

    b.bind(PORT);

如果我想监控 activity 两个线程池 bossGroup 和 workerPool,比如活动线程数和池大小?我应该怎么做?

对于Java的ThreadPoolExecutor,我有:

ThreadPoolExecutor.getActiveCount()
ThreadPoolExecutor.getQueue.size() 

对于EventLoopGroup,它也扩展了ExecutorService。有没有办法获取这些信息?

如果你使用 NioEventLoopGroup / EpollEventLoopGroup / KQueueEventLoopGroup 你可以使用 SingleThreadEventExecutor.pendingTasks() :

https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java#L308

您可以通过以下方式获取所有执行器:

EventLoopGroup.iterator()