Netty in the life cycle of ChannelHandler

When using Netty network programming often required at different stages of operation corresponding network connection, such as connection establishment, the client initiates an authentication to the server, parsing the content data and the like when receiving data. So, how to represent the different stages connected in netty in it? This is the discussion in this article, Netty in ChannelHandller life cycle.

First, we analyze the life cycle of small-Fi connection is established ---> data exchange ---> disconnected, the data exchange phase, including reading data from and writing data to connect the connection. Know the lifetime of the connection, you can find what you want you want to operate at all stages. In Netty, a different life cycle of the network connection can be used to bind the corresponding logical way through the callback, the callback interface is ChannelHandler, we are here mainly to ChannelInboundHandleran example for analysis. In ChannelInboundHandlerdefined as follows and life-cycle-related interfaces:

  • channelRegistered
  • channelUnregistered
  • channelActive
  • channelInactive
  • channelRead
  • channelReadComplete

With two defined in the parent class of ``:

  • handlerAdded
  • handlerRemoved

What these calls Preface callback interface is it? By writing us a LifeCycleHandlerlook at the order life cycle ChannelInboundHandler under.

public class LifeCycleInBoundHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("channelRegistered: channel注册到NioEventLoop");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("channelUnregistered: channel取消和NioEventLoop的绑定");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("channelActive: channel准备就绪");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("channelInactive: channel被关闭");
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) 
            throws Exception {
        out.println("channelRead: channel中有可读的数据" );
        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("channelReadComplete: channel读数据完成");
        super.channelReadComplete(ctx);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("handlerAdded: handler被添加到channel的pipeline");
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) 
            throws Exception {
        out.println("handlerRemoved: handler从channel的pipeline中移除");
        super.handlerRemoved(ctx);
    }
}

Client and server started, after a successful connection, the client is disconnected, the output terminal Server as follows:

handlerAdded: handler被添加到channel的pipeline
channelRegistered: channel注册到NioEventLoop
channelActive: channel准备就绪
channelRead: channel中有可读的数据
channelReadComplete: channel读数据完成
channelReadComplete: channel读数据完成
channelInactive: channel被关闭
channelUnregistered: channel取消和NioEventLoop的绑定
handlerRemoved: handler从channel的pipeline中移除

From the above results can know, from establishing connections to disconnect, handler lifecycle callback interface calls in the following order:

handlerAdded -> channelRegistered 
-> channelActive -> channelRead -> channelReadComplete
-> channelInactive -> channelUnRegistered -> handlerRemoved

The following specifically the specific meaning of each callback:

  1. handlerAdded: new connections can be made in accordance with the initialization policy, the handler is added to the channel inside the pipeline, which is channel.pipeline.addLast (new LifeCycleInBoundHandler) callback after completion;
  2. channelRegistered: When the connection is assigned to a specific worker thread, the callback will be called.
  3. channelActive: channel of the preparatory work has been completed, the addition is complete all the pipeline, and assigned to a specific line, indicating that the channel is ready for use.
  4. channelRead: Client to send data to the server, each time the callback method, data is being read;
  5. channelReadComplete: the server each time after reading a complete data, the callback method, data indicating the reading is completed;
  6. channelInactive: When disconnected, the callback will be called, indicating that this time the underlying TCP connection has been disconnected.
  7. channelUnREgistered: corresponds channelRegistered, when the connection is closed, the release of the thread-bound workder;
  8. handlerRemoved: corresponds handlerAdded, the handler callback method of the channel is removed from the pipeline.

Guess you like

Origin www.cnblogs.com/yuanged/p/12595977.html