Spring Boot关闭钩子

Abd*_*ull 38 java spring-boot

如何注册/添加一个自定义关闭例程,该例程将在我的Spring Boot应用程序关闭时触发?

场景:我将Spring Boot应用程序部署到Jetty servlet容器(即没有嵌入式Jetty).我的应用程序使用Logback进行日志记录,我想使用Logback的MBean JMX配置程序在运行时更改日志记录级别.其文档指出,为避免内存泄漏,在关闭时必须调用特定的LoggerContext关闭方法.

听取Spring Boot关闭事件的好方法是什么?

我试过了:

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext cac = SpringApplication.run(Example.class, args);

    cac.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {

        @Override
        public void onApplicationEvent(ContextClosedEvent event) {
            logger.info("Do something");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但是当应用程序关闭时,不会调用此注册的侦听器.

cfr*_*ick 25

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit

每个SpringApplication都会向JVM注册一个关闭钩子,以确保在退出时正常关闭ApplicationContext.可以使用所有标准的Spring生命周期回调(例如DisposableBean接口或@PreDestroy注释).

此外,如果bean希望在应用程序结束时返回特定的退出代码,则bean可以实现org.springframework.boot.ExitCodeGenerator接口.

  • 更新链接 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-exit (2认同)

Dav*_*yer 7

您的监听器注册太晚(在上下文已关闭之前永远不会到达该行).它应该足以成为一个@Bean.


小智 5

您是否尝试过@cfrick提到的方法?

@SpringBootApplication
@Slf4j
public class SpringBootShutdownHookApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringBootShutdownHookApplication.class, args);
  }

  @PreDestroy
  public void onExit() {
    log.info("###STOPing###");
    try {
      Thread.sleep(5 * 1000);
    } catch (InterruptedException e) {
      log.error("", e);;
    }
    log.info("###STOP FROM THE LIFECYCLE###");
  }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*ran 5

我有一个类似的用例,我必须将服务器的关闭过程保持几分钟,我使用了问题中提到的相同方法,唯一的变化是不是在启动服务后添加监听器,而是添加了监听器( ContextClosedEvent) 运行应用程序之前

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners((ApplicationListener<ContextClosedEvent>) event -> {
            log.info("Shutdown process initiated...");
            try {
                Thread.sleep(TimeUnit.MINUTES.toMillis(5));
            } catch (InterruptedException e) {
                log.error("Exception is thrown during the ContextClosedEvent", e);
            }
            log.info("Graceful Shutdown is processed successfully");
        });
        application.run(args);
    }
}
Run Code Online (Sandbox Code Playgroud)