ChannelHandler生命周期

Channel生命周期

  • public void channelUnregistered(ChannelHandlerContext ctx) throws Exception
    Channel已经被创建,但还未注册到EventLoop,或Channel和EventLoop解除绑定

  • public void channelRegistered(ChannelHandlerContext ctx) throws Exception
    当Channel成功绑定一个EventLoop线程后,通过pipeline回调所有的Handler的channelRegistered()

  • public void channelActive(ChannelHandlerContext ctx) throws Exception
    当Channel添加Handler完成、绑定EventLoop完成后,通过pipeline回调所有Handler的channelActive()

  • public void channelInactive(ChannelHandlerContext ctx) throws Exception
    Channel连接已经关闭

ChannelHandler生命周期

  • public void handlerAdded(ChannelHandlerContext ctx) throws Exception
    当自定义Handler加入到pipeline后被回调,也就是在pipeline.addLast()后被回调

  • public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
    pipeline移除Handler

  • public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
    pipeline中发生异常时被调用

Tips

  • SimpleChannelInboundHandler会自动释放资源
  • SimpleChannelInboundHandler需要手动释放资源调用ReferenceCountUtil.release(msg);释放资源

你可能感兴趣的:(ChannelHandler生命周期)