常见Java应用如何优雅关闭

开发 前端
在我们进行系统升级的时候,往往需要关闭我们的应用,然后重启。在关闭应用前,我们希望做一些前置操作,比如关闭数据库、redis连接,清理zookeeper的临时节点,释放分布式锁,持久化缓存数据等等。

一、前言

在我们进行系统升级的时候,往往需要关闭我们的应用,然后重启。在关闭应用前,我们希望做一些前置操作,比如关闭数据库、redis连接,清理zookeeper的临时节点,释放分布式锁,持久化缓存数据等等。

二、Linux的信号机制

在linux上,我们关闭进程主要是使用 kill 的方式。

当执行该命令以后,linux会向进程发送一个信号,进程收到以后之后,可以做一些清理工作。

kill 命令默认的信号值为 15 ,即 SIGTERM 信号。

通过 kill -l 查看linux支持哪些信号:

常见Java应用如何优雅关闭

linux提供了 signal() api,可以将信号处理函数注册上去:

 

  1. #include <signal.h> 
  2. #include <stdio.h> 
  3. #include <unistd.h> 
  4. #include <stdlib.h> 
  5. #include <stdbool.h> 
  6.  
  7. static void gracefulClose(int sig)   
  8.     printf("执行清理工作\n"); 
  9.     printf("JVM 已关闭\n"); 
  10.     exit(0);    //正常关闭 
  11.  
  12. int main(int argc,char *argv[])   
  13.     if(signal(SIGTERM,gracefulClose) == SIG_ERR) 
  14.         exit(-1); 
  15.  
  16.     printf("JVM 已启动\n"); 
  17.  
  18.     while(true
  19.     { 
  20.         // 执行工作 
  21.         sleep(1); 
  22.     } 

三、Java提供的Shutdown Hook

Java并不支持类似于linux的信号机制,但是提供了 Runtime.addShutdownHook(Thread hook) 的api。

在JVM关闭前,会并发执行各个Hook线程。

 

  1. public class ShutdownHook { 
  2.  
  3.     public static void main(String[] args) throws InterruptedException { 
  4.         Runtime.getRuntime().addShutdownHook(new DbShutdownWork()); 
  5.         System.out.println("JVM 已启动"); 
  6.  
  7.         while(true){ 
  8.             Thread.sleep(10L); 
  9.         } 
  10.     } 
  11.  
  12.     static class DbShutdownWork extends Thread{ 
  13.         public void run(){ 
  14.             System.out.println("关闭数据库连接"); 
  15.         } 
  16.     } 

四、Spring Boot提供的优雅关闭功能

我们一般采用如下的方式,启动一个Spring boot应用:

 

  1. public static void main(String[] args) throws Exception {   
  2.     SpringApplication.run(SampleController.class, args); 

SpringApplication.run()代码如下,会调用到refreshContext(context)方法:

 

  1. public ConfigurableApplicationContext run(String... args) {   
  2.     StopWatch stopWatch = new StopWatch(); 
  3.     stopWatch.start(); 
  4.     ConfigurableApplicationContext context = null
  5.     FailureAnalyzers analyzers = null
  6.     configureHeadlessProperty(); 
  7.     SpringApplicationRunListeners listeners = getRunListeners(args); 
  8.     listeners.started(); 
  9.     try { 
  10.         ApplicationArguments applicationArguments = new DefaultApplicationArguments( 
  11.                 args); 
  12.         ConfigurableEnvironment environment = prepareEnvironment(listeners, 
  13.                 applicationArguments); 
  14.         Banner printedBanner = printBanner(environment); 
  15.         context = createApplicationContext(); 
  16.         analyzers = new FailureAnalyzers(context); 
  17.         prepareContext(context, environment, listeners, applicationArguments, 
  18.                 printedBanner); 
  19.         refreshContext(context); 
  20.         afterRefresh(context, applicationArguments); 
  21.         listeners.finished(context, null); 
  22.         stopWatch.stop(); 
  23.         if (this.logStartupInfo) { 
  24.             new StartupInfoLogger(this.mainApplicationClass) 
  25.                     .logStarted(getApplicationLog(), stopWatch); 
  26.         } 
  27.         return context; 
  28.     } 
  29.     catch (Throwable ex) { 
  30.         handleRunFailure(context, listeners, analyzers, ex); 
  31.         throw new IllegalStateException(ex); 
  32.     } 

refreshContext()方法比较简单:

 

  1. private void refreshContext(ConfigurableApplicationContext context) {   
  2.     refresh(context);   //调用ApplicationContext.refresh() 
  3.     if (this.registerShutdownHook) {        //registerShutdownHook默认值为true 
  4.         try { 
  5.             context.registerShutdownHook(); 
  6.         } 
  7.         catch (AccessControlException ex) { 
  8.             // Not allowed in some environments. 
  9.         } 
  10.     } 

AbstractApplicationContext.registerShutdownHook()代码:

 

  1. public void registerShutdownHook() {   
  2.     if (this.shutdownHook == null) { 
  3.         this.shutdownHook = new Thread() { 
  4.             @Override 
  5.             public void run() { 
  6.                 synchronized (startupShutdownMonitor) { 
  7.                     doClose(); 
  8.                 } 
  9.             } 
  10.         }; 
  11.         Runtime.getRuntime().addShutdownHook(this.shutdownHook); 
  12.     } 

很明显,Spring boot通过在启动时,向JVM注册一个ShutdownHook,从而实现JVM关闭前,正常关闭Spring容器。而Spring在销毁时,会依次调用bean的destroy动作来销毁。

五、Dubbo的优雅关闭策略

Dubbo同样是基于ShutdownHook实现的。

AbstractConfig的static代码:

 

  1. static {   
  2.     Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
  3.         public void run() { 
  4.             if (logger.isInfoEnabled()) { 
  5.                 logger.info("Run shutdown hook now."); 
  6.             } 
  7.             ProtocolConfig.destroyAll(); 
  8.         } 
  9.     }, "DubboShutdownHook")); 

六、总结

只要我们的应用运行在linux平台上,所有的优雅关闭方案都是基于linux提供的信号机制提供的,JVM也是如此。

Java并没有为我们提供与之一一对应的api,而是给出了个ShutdownHook机制,也能达到类似的效果,缺点是我们无法得知JVM关闭的原因。

像dubbo、spring boot等成熟的开源框架,都实现了自动注册ShutdownHook的功能,从而避免使用者忘记调用优雅关闭api引发问题,降低框架的使用难度。

责任编辑:未丽燕 来源: 点我达技术
相关推荐

2021-01-19 10:35:49

JVM场景函数

2022-04-11 08:17:07

JVMJava进程

2021-03-28 09:17:18

JVM场景钩子函数

2021-04-20 08:00:31

Redisson关闭订单支付系统

2021-12-06 09:57:25

容器Linux信号

2021-01-18 13:17:04

鸿蒙HarmonyOSAPP

2021-01-28 14:53:19

PHP编码开发

2022-06-02 10:02:47

Kubectl更新应用Linux

2023-12-20 10:04:45

线程池Java

2022-09-08 07:32:56

JDK7程序管理

2023-10-10 13:23:18

空指针异常Java

2019-11-18 15:50:11

AjaxJavascript前端

2021-07-15 09:47:20

Docker容器命令

2022-01-10 09:35:50

日志语言解析器

2022-03-07 07:33:24

Spring自定义机制线程池

2020-12-08 08:08:51

Java接口数据

2020-09-25 11:30:20

Java判空代码

2022-09-15 07:31:49

Spring拦截器注解

2015-11-26 10:53:45

LinuxWindowsMac OS

2017-07-26 11:32:50

NETRabbitMQ系统集成
点赞
收藏

51CTO技术栈公众号