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()
有什么不同。当通道关闭时,将调用两种方法。
两种方法都能达到相同的效果吗?