欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

netty应用退出

程序员文章站 2022-04-22 21:54:50
...

netty程序,使用shutdownGracefully退出。退出前会把队列中的消息发送完,释放channel,多路复用器的去注册,清空定时器任务等。

    public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
        EventExecutor[] var6 = this.children;
        int var7 = var6.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            EventExecutor l = var6[var8];
            l.shutdownGracefully(quietPeriod, timeout, unit);
        }

        return this.terminationFuture();
    }

shutdownGracefully实现在NioEventLoop的父类SingleThreadEventExecutor中,

首先是改变state,为了防止并发问题,这里是netty 4,采用的原子类自旋的方式避免加锁。

public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
        ObjectUtil.checkPositiveOrZero(quietPeriod, "quietPeriod");
        if(timeout < quietPeriod) {
            throw new IllegalArgumentException("timeout: " + timeout + " (expected >= quietPeriod (" + quietPeriod + "))");
        } else {
            ObjectUtil.checkNotNull(unit, "unit");
            if(this.isShuttingDown()) {
                return this.terminationFuture();
            } else {
                boolean inEventLoop = this.inEventLoop();

                boolean wakeup;
                int oldState;
                int newState;
                do {
                    if(this.isShuttingDown()) {
                        return this.terminationFuture();
                    }

                    wakeup = true;
                    oldState = this.state;
                    if(inEventLoop) {
                        newState = 3;
                    } else {
                        switch(oldState) {
                        case 1:
                        case 2:
                            newState = 3;
                            break;
                        default:
                            newState = oldState;
                            wakeup = false;
                        }
                    }
                } while(!STATE_UPDATER.compareAndSet(this, oldState, newState));

                this.gracefulShutdownQuietPeriod = unit.toNanos(quietPeriod);
                this.gracefulShutdownTimeout = unit.toNanos(timeout);
                if(this.ensureThreadStarted(oldState)) {
                    return this.terminationFuture;
                } else {
                    if(wakeup) {
                        this.taskQueue.offer(WAKEUP_TASK);
                        if(!this.addTaskWakesUp) {
                            this.wakeup(inEventLoop);
                        }
                    }

                    return this.terminationFuture();
                }
            }
        }
    }

唤醒selector,由NioEventLoop实现。

 protected void wakeup(boolean inEventLoop) {
        if(!inEventLoop && this.nextWakeupNanos.getAndSet(-1L) != -1L) {
            this.selector.wakeup();
        }

    }

状态改变之后, run方法中检测到状态变化,执行closeAll方法

if(this.isShuttingDown()) {
                        this.closeAll();
                        if(this.confirmShutdown()) {
                            return;
                        }
                    }
相关标签: netty