Netty核心概念
Bootstrap、ServerBootstrap
Future、ChannelFuture
Selector
ChannelHandler
childHander与hander区别
hander用于处理bossGroup,childHander用于处理workGroup
ChannelPipeline
1.Channel中包含一个ChannelPipeline
2.ChannelPipeline维护着一个ChannelHandlerContext的双向链表
3.ChannelHandlerContext关联着一个ChannelHandler
实例中:
ChannelPipeline首先加入一个ChanelHandlerContext(关联ChannelInitializer的channel handler)
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup(2);
try{
ServerBootstrap boot = new ServerBootstrap();
boot.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyServerHandler());
}
});
System.out.println("server is ready");
ChannelFuture sync = boot.bind(6666).sync();
sync.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
ChannelHandlerContext
ChannelOption