Spring Boot shutdown hook

后端 未结 4 2068
醉话见心
醉话见心 2020-12-05 12:59

How can I register/add a custom shutdown routine that shall fire when my Spring Boot application shuts down?

Scenario: I deploy my Spring Boot application to a Jetty

相关标签:
4条回答
  • 2020-12-05 13:29

    have you tried this as mentioned by @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###");
      }
    }
    
    0 讨论(0)
  • 2020-12-05 13:34
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    @EnableAutoConfiguration
    public class Application extends SpringBootServletInitializer {
    
        public static void main(
                String[] args) {
            SpringApplication.run(Application.class,
                                  args);
        }
    
        @NotNull
        @Bean
        ServletListenerRegistrationBean<ServletContextListener> myServletListener() {
            ServletListenerRegistrationBean<ServletContextListener> srb =
                    new ServletListenerRegistrationBean<>();
            srb.setListener(new ExampleServletContextListener());
            return srb;
        }
    }
    
     import javax.servlet.ServletContextEvent;
     import javax.servlet.ServletContextListener;
    
     public class ExampleServletContextListener implements ServletContextListener {
        @Override
        public void contextInitialized(
            ServletContextEvent sce) {
        // Context Initialised
        }
    
        @Override
        public void contextDestroyed(
            ServletContextEvent sce) {
           // Here - what you want to do that context shutdown    
       }
    }
    
    0 讨论(0)
  • 2020-12-05 13:35

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

    Each SpringApplication will register a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit. All the standard Spring lifecycle callbacks (such as the DisposableBean interface, or the @PreDestroy annotation) can be used.

    In addition, beans may implement the org.springframework.boot.ExitCodeGenerator interface if they wish to return a specific exit code when the application ends.

    0 讨论(0)
  • 2020-12-05 13:43

    Your listener is registered too late (that line will never be reached until the context has already closed). It should suffice to make it a @Bean.

    0 讨论(0)
提交回复
热议问题