美文网首页
EventListener监听器源码浅析

EventListener监听器源码浅析

作者: zZeroZz | 来源:发表于2020-05-31 19:35 被阅读0次

    1.AbstractBootstrap

    private static void doBind0(final ChannelFuture regFuture, final Channel channel, final SocketAddress localAddress, final ChannelPromise promise) {
    channel.eventLoop().execute(new Runnable() {
    public void run() {
    if (regFuture.isSuccess()) {
    channel.bind(localAddress,
    // 此处加了监听器
    promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
    } else {
    promise.setFailure(regFuture.cause());
    }

            }
        });
    }
    

    2. AbstractChannel.AbstractUnsafe

    public final void bind(SocketAddress localAddress, ChannelPromise promise) {
               ....
    
                    // 此处调用了
                    this.safeSetSuccess(promise);
                }
            }
    

    3.AbstractChannel

    protected final void safeSetSuccess(ChannelPromise promise) {
    // trySuccess
                if (!(promise instanceof VoidChannelPromise) && !promise.trySuccess()) {
                    AbstractChannel.logger.warn("Failed to mark a promise as success because it is done already: {}", promise);
                }
    
            }
    

    DefaultChannelPromise

    public boolean trySuccess() {
            return this.trySuccess((Object)null);
        }
    

    DefaultPromise

    public boolean trySuccess(V result) {
            if (this.setSuccess0(result)) {
                this.notifyListeners();
                return true;
            } else {
                return false;
            }
        }
    
    private void notifyListeners() {
            EventExecutor executor = this.executor();
            if (executor.inEventLoop()) {
                InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get();
                int stackDepth = threadLocals.futureListenerStackDepth();
                if (stackDepth < MAX_LISTENER_STACK_DEPTH) {
                    threadLocals.setFutureListenerStackDepth(stackDepth + 1);
    
                    try {
    // 这里
                        this.notifyListenersNow();
                    } finally {
                        threadLocals.setFutureListenerStackDepth(stackDepth);
                    }
    
                    return;
                }
            }
    
            safeExecute(executor, new Runnable() {
                public void run() {
                    DefaultPromise.this.notifyListenersNow();
                }
            });
        }
    
    // 再一路
    private static void notifyListener0(Future future, GenericFutureListener l) {
            try {
    // 最终就是这个,最后调用了事件
                l.operationComplete(future);
            } catch (Throwable var3) {
                logger.warn("An exception was thrown by " + l.getClass().getName() + ".operationComplete()", var3);
            }
    
        }
    

    相关文章

      网友评论

          本文标题:EventListener监听器源码浅析

          本文链接:https://www.haomeiwen.com/subject/gdprzhtx.html