zoukankan      html  css  js  c++  java
  • Netty:ChannelInitializer

    1. 作用

      用于在某个Channel注册到EventLoop后,对这个Channel执行一些初始化操作。ChannelInitializer虽然会在一开始会被注册到Channel相关的pipeline里,但是在初始化完成之后,ChannelInitializer会将自己从pipeline中移除,不会影响后续的操作。

    2. 实现原理

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            if (ctx.channel().isRegistered()) {
                // This should always be true with our current DefaultChannelPipeline implementation.
                // The good thing about calling initChannel(...) in handlerAdded(...) is that there will be no ordering
                // suprises if a ChannelInitializer will add another ChannelInitializer. This is as all handlers
                // will be added in the expected order.
                initChannel(ctx);
            }
        }
    
        @SuppressWarnings("unchecked")
        private boolean initChannel(ChannelHandlerContext ctx) throws Exception {
            if (initMap.putIfAbsent(ctx, Boolean.TRUE) == null) { // Guard against re-entrance.
                try {
                    initChannel((C) ctx.channel());
                } catch (Throwable cause) {
                   
                    exceptionCaught(ctx, cause);
                } finally {
                    // 移除自己
                    remove(ctx);
                }
                return true;
            }
            return false;
        }
    
        private void remove(ChannelHandlerContext ctx) {
            try {
                ChannelPipeline pipeline = ctx.pipeline();
                if (pipeline.context(this) != null) {
                    pipeline.remove(this);
                }
            } finally {
                initMap.remove(ctx);
            }
        }
  • 相关阅读:
    微信小程序——Now you can provide attr "wx:key" for a "wx:for" to improve performance.
    mac 桌面美化
    获取json对象长度
    js 记忆函数
    js提取整数部分,移除首末空格
    js糟粕
    zepto 获取checked selected元素
    BFC块级排版上下文
    zepto 获取select选中的值
    手机不支持onchange事件
  • 原文地址:https://www.cnblogs.com/virgosnail/p/10576139.html
Copyright © 2011-2022 走看看