This section describes Netty components

Because Netty is used for architecture or middleware communication, so I have not directly based on Netty programming, so I first read the user manual, found that the user manual Netty summarizes the basic concepts of two or three sentences, so I first studied the basic concepts of Netty core components, and then read the source code.

Start by ditching the service code

Next to abandon the services in the user manual (only receives data does not make any response) code, these two classes of basic covers the basic use of netty, I will comment in the code, and then start to explain its core component, if understanding which components don’t need my detailed instructions, can also tell me, I can in the subsequent detailed introduction, However, this article only introduces the basic concepts of components, which will be detailed in the source code later.

package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class DiscardServer {
    private int port;

    public DiscardServer(int port) {
        this.port = port;
    }

    public void run(a) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); / / (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();/ / (1)
        try {
            // Netty's service enablement class
            ServerBootstrap b = new ServerBootstrap(); 
            b.group(bossGroup, workerGroup) / / (1)
                    Netty writes a ServerSocketChannel to receive connection requests
                    .channel(NioServerSocketChannel.class) 
                    // It is understandable that we added a handler to SocketChannel to handle it
                    .childHandler(new ChannelInitializer<SocketChannel>() { 
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new DiscardServerHandler()); / / (2)}})// Configure NioServerSocketChannel
                    .option(ChannelOption.SO_BACKLOG, 128)  
                    // Configure SocketChannel
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            // The bound port starts receiving requests
            ChannelFuture f = b.bind(port).sync();
            // Shut down the service
            f.channel().closeFuture().sync();
        } finally{ workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); }}public static void main(String[] args) throws Exception {
        int port = 8080;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        }

        newDiscardServer(port).run(); }}Copy the code
package netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/** * Custom processing of I/O requests */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {/ / (3)

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // Discard the received data.
        ((ByteBuf) msg).release(); 
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception occurs.cause.printStackTrace(); ctx.close(); }}Copy the code

The basic concepts and understanding of code comments (1) and (2) above are explained in detail in the next two chapters.