content: package com.expectlost.netty;
  
  
  import io.netty.bootstrap.ServerBootstrap;
  import io.netty.channel.ChannelFuture;
  import io.netty.channel.ChannelInitializer;
  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 AppServer {
      private int port;
  
      public AppServer(int port) {
          this.port = port;
      }
  
      public void start()
      {
          //TODO 1 创建eventLoop 老板只负责处理请求 之后会将请求分发至worker
          //TODO 工人是老板的5倍
          EventLoopGroup boss = new NioEventLoopGroup(2);
          EventLoopGroup worker = new NioEventLoopGroup(5);
  
          try {
              //TODO 创建服务器引导器
              ServerBootstrap serverBootstrap = new ServerBootstrap();
              serverBootstrap = serverBootstrap.group(boss, worker)
                      .channel(NioServerSocketChannel.class)
                      .childHandler(new ChannelInitializer<SocketChannel>() {
                          @Override
                          protected void initChannel(SocketChannel socketChannel) throws Exception {
                              //TODO 使用处理器
                              socketChannel.pipeline().addLast(new MyChannelHandler());
                          }
                      });
  
              //TODO 绑定端口
              ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
              channelFuture.channel().closeFuture().sync();
          }catch (Exception e)
          {
  
          }finally {
              try {
                  boss.shutdownGracefully().sync();
                  worker.shutdownGracefully().sync();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
      }
  
      public static void main(String[] args) {
          new AppServer(8080).start();
      }
  }
  