您当前的位置: 首页 > 

庄小焱

暂无认证

  • 2浏览

    0关注

    805博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Netty——BootStrap源码分析

庄小焱 发布时间:2021-11-09 09:39:02 ,浏览量:2

摘要

BootStrap在netty的应用程序中负责引导服务器和客户端。netty包含了两种不同类型的引导:

  • 1. 使用服务器的ServerBootStrap,用于接受客户端的连接以及为已接受的连接创建子通道。
  • 2. 用于客户端的BootStrap,不接受新的连接,并且是在父通道类完成一些操作。
服务端的代码 SimpleServer.java
/**
 * Created by xjl on 2021/1/9.
 */
public final class SimpleServer {

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new SimpleServerHandler())
                    .childHandler(new SimpleServerInitializer())
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.bind(8888).sync();

            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
SimpleServerHandler.java
private static class SimpleServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelRegistered");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerAdded");
    }
}
SimpleServerInitializer.java
public class SimpleServerInitializer extends ChannelInitializer{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new SimpleChatServerHandler());

        System.out.println("SimpleChatClient:" + ch.remoteAddress()+"连接上");
    }
}
ServerBootstrap类分析
//使用了链式编程方式

ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new SimpleServerHandler())
                .childHandler(new SimpleServerInitializer())
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

ServerBootstrap类的继承结构如下:

该类的参数,有必要列出:

private final Map, Object>();
private final Map, Object>();
private volatile EventLoopGroup childGroup;
private volatile ChannelHandler childHandler; 

其父类AbstractBootstrap的参数

private volatile EventLoopGroup group;
private volatile ChannelFactory, Object> options = new LinkedHashMap, Object> attrs = new LinkedHashMap, Object> options = new LinkedHashMap            
关注
打赏
1657692713
查看更多评论
0.0369s