Basic concepts of Netty

Keywords: Programming Netty network socket

Boot

  1. BossGroup and WorkerGroup: the former is responsible for receiving client connections, and the latter is responsible for network reading and writing. All types are NioEventLoopGroup
  2. NioEventLoopGroup is equivalent to an event loop group, which contains multiple event loops. Each event loop is NioEventLoop
  3. NioEventLoop refers to a thread executing processing tasks in a continuous loop. Each NioEventLoop has a selector, which is used to listen for binding The network communication of the socket fixed on it.
  4. There are three steps for each Boss NioEventLoop loop loop
    1. Polling for accept events
    2. Handle the accept event, establish a connection with the client, generate the NioScocketChannel, and register it to a worker NIOEventLoop selector
    3. Tasks processing task queues
  5. Steps for each Worker NIOEventLoop loop loop:
    1. Polling read and write events
    2. Handle i/o events, i.e. read and write events, in the corresponding NioScocketChannel
    3. The task that processes the task queue, runAllTasks

CODE

Service

	//Create BossGroup and WorkerGroup
        //Explain
        //1. Create two thread groups: bossGroup and workerGroup
        //2. The bossgroup only processes connection requests. The real business processing with the client will be completed by the workerGroup
        //3. Both are infinite cycles
        //4. Number of sub threads (NioEventLoop) in bossgroup and workerGroup
        // Default actual cpu cores * 2
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        //Create server-side startup object and configure parameters
        ServerBootstrap bootstrap = new ServerBootstrap();
        //Use chain programming to set
        bootstrap.group(bossGroup, workerGroup) //Set up two thread groups
                .channel(NioServerSocketChannel.class) //Using NioSocketChannel as the channel of server
                .option(ChannelOption.SO_BACKLOG, 128) // Set thread queue to get the number of connections
                .childOption(ChannelOption.SO_KEEPALIVE, true) //Set keep active connection state
                .childHandler(new ChannelInitializer<socketchannel>() {//Create a channel test object (anonymous object)
                    //Set up the processor for the pipeline
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                    //To customize a Handler, we need to continue with a handleradapter specified by netty
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
                    }
                }); // Set the processor for the corresponding pipeline of EventLoop of our workerGroup
        System.out.println(".....The server is ready...");
        //Bind a port and synchronize to generate a ChannelFuture object
        //Start server (and bind port)
        ChannelFuture cf = bootstrap.bind(6668).sync();
        //Monitor the closed channel
        cf.channel().closeFuture().sync();

Client

        //Client needs an event loop group
        EventLoopGroup group = new NioEventLoopGroup();
        //Create client startup object
        //Note that the client uses Bootstrap instead of ServerBootstrap
        Bootstrap bootstrap = new Bootstrap();
        //Set related parameters
        bootstrap.group(group) //Set thread group
                .channel(NioSocketChannel.class) // Set the implementation class (reflection) of the client channel
                .handler(new ChannelInitializer<socketchannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                    //To customize a Handler, we need to continue with a handleradapter specified by netty
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter()); //Add your own processor
                    }
                });
        System.out.println("Client ok..");
        //Start the client to connect to the server
        //The analysis of ChannelFuture involves the asynchronous model of netty
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
```</socketchannel></socketchannel>

Posted by jaco on Fri, 27 Dec 2019 11:43:42 -0800