Netty中的channelInactive和channe.closeFuture()。addListener()有什么区别

问题描述 投票:0回答:1
 public class ChannelActiveHandler extends SimpleChannelInboundHandler {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel open");

        // add closeListener
        ctx.channel().closeFuture().addListener(future->{

            // do somthing when channel is close!
            System.out.println("channel close! state:"+ctx.channel().isActive());
        });

        super.channelActive(ctx);
    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        // do somthing when channel is close!
        System.out.println("channel inactive!");
        super.channelInactive(ctx);
    }

}

如上所述,Netty中的channelInactive()channel.closeFuture().addListener()有什么不同。当通道关闭时,将调用两种方法。

两种方法都能达到相同的效果吗?

java netty
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.