Netty的Channel和EventLoop

今天我系统地学习了NettyChannelEventLoop,掌握了Netty的事件循环模型,了解了Netty采用的多路复用器技术。下面是一个简单的客户端:

public class Client {

 

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

        EventLoopGroup group = new NioEventLoopGroup();

 

        try {

            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(group)

                    .channel(NioSocketChannel.class)

                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override

                        protected void initChannel(SocketChannel ch)

                                throws Exception {

                            ch.pipeline().addLast(new ClientHandler());

                        }

                    });

 

            ChannelFuture future = bootstrap.connect("localhost", 8080).sync();

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

        } finally {

            group.shutdownGracefully();

        }

    }

}

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