Netty框架 - 引导类Bootstrap

Bootstrap引导类

Bootstrap是Socket客户端创建工具类,通过Bootstrap可以创建Netty的客户端,并发起异步TCP连接操作。Bootstrap不是线程安全。

    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(group)
     .channel(NioSocketChannel.class)
     .option(ChannelOption.TCP_NODELAY, true)
     .handler(new ChannelInitializer<SocketChannel>() {
         @Override
         public void initChannel(SocketChannel ch) throws Exception {
             ChannelPipeline p = ch.pipeline();
             p.addLast(new LoggingHandler(LogLevel.INFO));
         }
     });
    ChannelFuture f = b.connect(HOST, PORT).sync();

Bootstrap继承结构

在这里插入图片描述

Bootstrap常用功能

1、设置EventLoopGroup线程池

2、TCP参数设置接口

ChannelOption参数

Netty源码分析- AbstractBootstrap类源码

    private final Map<ChannelOption<?>, Object> options = new LinkedHashMap();
   
    /**
     * Channel实例被创建时,指定使用的ChannelOption参数。
     * value参数为null,则移除这个ChannelOption参数
     * 
     * Allow to specify a {@link ChannelOption} which is used for the {@link Channel}
     * instances once they got created.
     * Use a value of {@code null} to remove a previous set {@link ChannelOption}.
     */
    public <T> B option(ChannelOption<T> option, T value) {
        //判空
        if (option == null) {
            throw new NullPointerException("option");
        } else {
            if (value == null) {
                //从map中移除
                synchronized(this.options) {
                    this.options.remove(option);
                }
            } else {
                //添加到map
                synchronized(this.options) {
                    this.options.put(option, value);
                }
            }

            return this.self();
        }
    }

3、添加和设置应用ChannelHandler接口

Bootstrap提供了ChannelInitializer简化ChannelHandler的编排,它继承了ChannelHandlerAdapter.

Netty源码分析-ChannelInitializer类源码

    /**
     * Channel被注册时,这个方法会被调用。
     * 方法返回后,这个实例将,从ChannelPipeline上移除
     * 
     * This method will be called once the {@link Channel} was registered. After the method returns this instance
     * will be removed from the {@link ChannelPipeline} of the {@link Channel}.
     **/
    protected abstract void initChannel(C var1) throws Exception;

    //TCP链路注册成功后,调用initChannel接口,用于设置用户ChannelHandler.
    private boolean initChannel(ChannelHandlerContext ctx) throws Exception {
        // Guard against re-entrance.
        if (initMap.putIfAbsent(ctx, Boolean.TRUE) == null) { 
            try {
                //将应用自定义的ChannelHandler添加到ChannelPipeline中
                initChannel((C) ctx.channel());
            } catch (Throwable cause) {
                // Explicitly call exceptionCaught(...) as we removed the handler before calling initChannel(...).
                // We do so to prevent multiple calls to initChannel(...).
                exceptionCaught(ctx, cause);
            } finally {
            	//从ChannelPipeline中移除
                remove(ctx);
            }
            return true;
        }
        return false;
    }

ChannelInitializer继承结构

ChannelInitializer继承结构

4、调用 Connect连接服务端


–思想–
专心协商大事时,别被琐事烦扰。
不要被分歧束缚住–要么提交上级裁定,要么投票表决!

发布了32 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/okxuewei/article/details/104863565