Netty的线程模型和Reactor模式

今天我学习了Netty的线程模型和Reactor模式,掌握了NettyIO线程池和业务线程池的设计和使用。下面是一个使用EventExecutorGroup的服务器:

public class Server {

 

    public static void main(String[] args) throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup();

        EventLoopGroup workerGroup = new NioEventLoopGroup();

        EventExecutorGroup businessGroup = new DefaultEventExecutorGroup(2);

 

        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup, workerGroup)

                    .channel(NioServerSocketChannel.class)

                    .childHandler(new ChannelInitializer<SocketChannel>() {

                        @Override

                        protected void initChannel(SocketChannel ch)

                                throws Exception {

                            ch.pipeline().addLast(new BusinessHandler(businessGroup));

                        }

                    });

 

            ChannelFuture future = bootstrap.bind(8080).sync();

            future.channel().closeFuture().sync();

        } finally {

            bossGroup.shutdownGracefully();

            workerGroup.shutdownGracefully();

            businessGroup.shutdownGracefully();

        }

    }

}

 

posted @ 2023-05-22 21:20  ITJAMESKING  阅读(10)  评论(0编辑  收藏  举报