dubbo的优雅关机真的优雅吗?

Created on 10 Aug 2017  ·  5Comments  ·  Source: apache/dubbo

个人观点:
1.可以拒绝新的请求,还算优雅。
2.等待一定的时间,然后强制停止。真的能停止吗?
欢迎大家讨论。

Most helpful comment

一般服务会设置超时时间,超过最大超时时间强制停止感觉没什么问题~

All 5 comments

一般服务会设置超时时间,超过最大超时时间强制停止感觉没什么问题~

其实我想我说的是dubbo的优雅关机其实就是调用线程池的shutDownNow方法。但是如果你的代码写的不够好,shutDownNow是不能把你怎么样的。
在ExecutorUtil类的优雅关机方法:

 public static void gracefulShutdown(Executor executor, int timeout) {
        if (!(executor instanceof ExecutorService) || isShutdown(executor)) {
            return;
        }
        final ExecutorService es = (ExecutorService) executor;
        try {
            es.shutdown(); // Disable new tasks from being submitted
        } catch (SecurityException ex2) {
            return ;
        } catch (NullPointerException ex2) {
            return ;
        }
        try {
            if(! es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {
                es.shutdownNow();
            }
        } catch (InterruptedException ex) {
            es.shutdownNow();
            Thread.currentThread().interrupt();
        }
        if (!isShutdown(es)){
            newThreadToCloseExecutor(es);
        }
    }

shutDownNow方法最终调用的线程的interrupt方法。interrupt方法只是给线程设置了一个中断标志位,然后还能打断sleep ,wait,join方法,使这些方方法抛异常,
但是我们总不能随随便便就调用sleep,wait,join方法吧。
所以我说如果你的代码写的不够好,shutDownNow方法对你是没有效果的。
你可以这样:
if(Thread.curentThread().isInterrupted()){
// throw new Exception();然后就可以中断了。
}

可以参考下:
http://blog.csdn.net/abountwinter/article/details/75277519

正好解决了个优雅停机问题,发表下意见:
我有个2个线程池,一个是接收mq消息写任务表的,另一个是不断扫任务表,然后执行业务逻辑。发现shutdownNow+awaitTermination只对mq线程池有效,后来发现是sql语句执行是不会响应中断的(除非超时),死循环中加个sleep就好了。

所以,对于能够响应中断的后台服务(比如接收mq消息),本身有throw InterruptException的,只要catch住后退出循环就好;一般无法shutdownNow的都是那种做成死循环或者没设置超时导致无法响应中断的,自己在死循环中加上sleep(sleep能响应中断)或者加上超时break。

其实就是要求你的代码中有这样的判断:
if(Thread.curentThread().isInterrupted()){
// throw new Exception();然后就可以中断了。
}
如果无故加sleeep ,wait,join等可以被打断的方法,代码不是很优雅

I agree with @panhongliang . You can not force a thread to exit. Dubbo has done as much as it can to stop the threads. For those thread who do not respond to InterruptedException, there is no way to stop them.

Was this page helpful?
0 / 5 - 0 ratings