SpringBoot优雅退出

栏目: Java · 发布时间: 6年前

内容简介:背景调用入口调用SpringApplication开始启动SpringBoot

背景

我看了一些介绍SpringBoot源码的文章,里面都是从 SpringApplication这个外部调用入口开始,重点介绍里面的listener监听器。今天我想想说说SpringBoot的优雅退出机制。

调用入口调用SpringApplication开始启动SpringBoot


 

@SpringBootApplication

public class Application {

public static void main(String[] args) throws Exception {

SpringApplication.run(Application.class, args);

}

}

SpringApplication实例初始化时有以下操作


 

public SpringApplication(Object... sources) {

this.bannerMode = Mode.CONSOLE;

this.logStartupInfo = true;

this.addCommandLineProperties = true;

this.headless = true;

this.registerShutdownHook = true;

this.additionalProfiles = new HashSet();

this.initialize(sources);

}

其中有一步 this.registerShutdownHook注册jvm退出前的事件。

概念

jvm有shutdwonHook机制,中文习惯叫优雅退出。相当于在 linux 系统中执行SIGTERM(kill -15 或者 svc -d)时退出前执行的一些操作。

原理

以linux系统为例说明:

进程在响应kill -15 pid命令进行关闭操作时默认发送SIGTERM信号至指定进程/进程组。如果进程没有捕获该信号的逻辑,则SIGTERM的作用是终止进程。而registerShutdownHook=true说明有注册的事件需要捕获该信号,先执行相应的逻辑再进行终止。


 

/**

* Register a shutdown hook with the JVM runtime, closing this context

* on JVM shutdown unless it has already been closed at that time.

* <p>Delegates to {@code doClose()} for the actual closing procedure.

* @see Runtime#addShutdownHook

* @see #close()

* @see #doClose()

*/

@Override

public void registerShutdownHook() {

if (this.shutdownHook == null) {

// No shutdown hook registered yet.

this.shutdownHook = new Thread() {

@Override

public void run() {

synchronized (startupShutdownMonitor) {

doClose();

}

}

};

Runtime.getRuntime().addShutdownHook(this.shutdownHook);

}

}

如上面spring里 registerShutdownHook的源码所示,就是注册一个jvm的shutdownHook钩子函数。jvm退出前会执行这个钩子函数。这个关闭操作在强制退出的时候不起作用。强制关闭范围详见下图

SpringBoot优雅退出

一个SpringBoot项目在优雅退出时做的事情可以通过log来感受一下


 

2019/06/20 20:47:20.405 Thread-29 [INFO] NettyHttpServer (NettyHttpServer.java:171) Close NettyHttpServer bind 0.0.0.0/0.0.0.0:5080


2019/06/20 20:47:20.405 Thread-36 [INFO] AnnotationConfigApplicationContext (AbstractApplicationContext.java:984) Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@a37aefe: startup date [Thu Jun 20 20:45:38 CST 2019]; root of context hierarchy


2019/06/20 20:47:20.406 Thread-32 [INFO] NettyServer (NettyServer.java:130) stopping netty server(XXX:9000), sleep 6 seconds!


2019/06/20 20:47:20.407 Thread-35 [INFO] ScheduleManager (ScheduleManager.java:373) Before taskNode quit,check executing task and callback result.


2019/06/20 20:47:20.407 Thread-35 [INFO] ScheduleManager (ScheduleManager.java:228) Destroy task node resources: zk and threadpool.


2019-06-20 20:47:20,430 Thread-35 WARN Unable to register Log4j shutdown hook because JVM is shutting down. Using SimpleLogger

总结

分析问题先找抓手

相关阅读

分布式架构之「 概念」

你看不懂的spring原理是因为不知道这几个概念

专治不会看源码的毛病--spring源码解析AOP篇(2017版)


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编程匠艺

编程匠艺

Pete Goodliffe / 韩江、陈玉 / 电子工业出版社 / 2011-11 / 85.00元

如果你可以编写出合格的代码,但是想更进一步、创作出组织良好而且易于理解的代码,并希望成为一名真正的编程专家或提高现有的职业技能,那么Pete Goodliffe编写的这本本书都会为你给出答案。本书的内容涵盖编程的各个要素,如代码风格、变量命名、错误处理和安全性等。此外,本书还对一些更广泛的编程问题进行了探讨,如有效的团队合作、开发过程和文档编写,等等。本书各章的末尾均提供一些思考问题,这些问题回顾......一起来看看 《编程匠艺》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具