refreshContext()办法比较简单:
- private void refreshContext(ConfigurableApplicationContext context) {
- refresh(context); //调用ApplicationContext.refresh()
- if (this.registerShutdownHook) { //registerShutdownHook默认值为true
- try {
- context.registerShutdownHook();
- }
- catch (AccessControlException ex) {
- // Not allowed in some environments.
- }
- }
- }
AbstractApplicationContext.registerShutdownHook()代码:
二、Linux的旌旗灯号机制
- public void registerShutdownHook() {
- if (this.shutdownHook == null) {
- this.shutdownHook = new Thread() {
- @Override
- public void run() {
- synchronized (startupShutdownMonitor) {
- doClose();
- }
- }
- };
- Runtime.getRuntime().addShutdownHook(this.shutdownHook);
- }
- }
很明显,Spring boot经由过程在启动时,向JVM注册一个ShutdownHook,大年夜而实现JVM封闭前,正常封闭Spring容器。而Spring在烧毁时,会依次调用bean的destroy动作来烧毁。
五、Dubbo的优雅封闭策略
Dubbo同样是基于ShutdownHook实现的。
AbstractConfig的static代码:
- static {
- Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
- public void run() {
- if (logger.isInfoEnabled()) {
- logger.info("Run shutdown hook now.");
- }
- ProtocolConfig.destroyAll();
- }
- }, "DubboShutdownHook"));
- }
六、总结
只要我们的应用运行在linux平台上,所有的优雅封闭筹划都是基于linux供给的旌旗灯号机制供给的,JVM也是如斯。
推荐阅读
买与卖2017年数据中间市场处于持续变更的状况。客户加快向云端迁徙带来的压力,再加上部件缺乏,使得数据中间>>>详细阅读
本文标题:常见Java应用如何优雅关闭
地址:http://www.17bianji.com/lsqh/39920.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示