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連接服務端


–思想–
專心協商大事時,別被瑣事煩擾。
不要被分歧束縛住–要麼提交上級裁定,要麼投票表決!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章