流量统计插件

    getty提供了简单的流量统计插件,可方便统计总的字节以及规定时间内的吞吐量

    1. AioServerStarter server = new AioServerStarter(8888);
    2. server.bossThreadNum(10).channelInitializer(new ChannelInitializer() {
    3. @Override
    4. public void initChannel(AioChannel channel) throws Exception {
    5. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    6. //添加统计插件
    7. ChannelTrafficShapingHandler channelTrafficShapingHandler= new ChannelTrafficShapingHandler(10000)
    8. defaultChannelPipeline.addLast(channelTrafficShapingHandler);
    9. //获取总读取字节数
    10. channelTrafficShapingHandler.getTotalRead();
    11. defaultChannelPipeline.addLast(new DelimiterFrameDecoder(DelimiterFrameDecoder.lineDelimiter));
    12. defaultChannelPipeline.addLast(new StringDecoder());
    13. defaultChannelPipeline.addLast(new SimpleHandler());
    14. }
    15. });
    16. server.start();

    构造方法

    1. //checkInterval为统计时间间隔,单位为毫秒
    2. ChannelTrafficShapingHandler(int checkInterval)

    目前仅提供各通道总的读写字节数以及单位时间内的吞吐量

    属性 类型 备注
    totalRead long 总读取字节
    totalWrite long 总写出字节
    intervalTotalRead long 时间间隔内读取的字节(吞吐量)
    intervalTotalWrite long 时间间隔内写出的字节(吞吐量)
    totalReadCount long 总读取次数
    totolWriteCount long 总写出次数