netty 详解

来源:互联网 发布:淘宝动态评分查询 编辑:程序博客网 时间:2024/06/06 01:13
  • ServerBootstrap.bind(new InetSocketAddress(port)) 服务端绑定端口
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); – Acceptor监听客户端的线程池
    NioEventLoopGroup workerGroup = new NioEventLoopGroup(2); – 处理io的线程池
    1) 初始化并注册Channel
    主要是根据bootstrap.channel(NioServerSocketChannel.class);来决定
  ChannelFuture initAndRegister() {      //1.生成Channel实例      Channel channel = NioServerSocketChannel.class.newInStance();      //2. 注册ServerBootstrapAcceptor      ChannelPipeline p = channel.pipeline();         p.addLast(new ChannelInitializer<Channel>() {           public void initChannel(Channel ch) throws Exception {               ch.pipeline().addLast(new ServerBootstrapAcceptor(                        childGroup, childHandler, childOptions, childAttrs));           }      });        //3. 将这个channel注册到bossGroup 里      ChannelFuture regFuture = bossGroup.register(channel);   } 
 小注:
    NioEventLoopGroup ... {        private final EventExecutor[] children ;        AtomicInteger childIndex = new AtomicInteger();        NioEventLoopGroup(int size) {             children  = new EventExecutor[size];             for (int i = 0; i < nThreads; i ++) {                children[i] = new NioEventLoop(this, threadFactory...);             }        }        public EventExecutor next() {            return children[childIndex.getAndIncrement() & children.length - 1];        }        public ChannelFuture register(Channel channel) {            return next().register(channel);        }    }

http://ifeve.com/paper-set/

0 0
原创粉丝点击