简体   繁体   English

如何捕捉关闭tomcat的事件?

[英]how to catch the event of shutting down of tomcat?

我想要的是每次tomcat服务器启动时都启动一个线程。为此,我需要捕获关闭tomcat的事件。我可以这样做吗?我尝试使用会话来做但有时候会话甚至在关闭后仍然存在下来并重申tomcat?我的选择是什么?

You can try to catch an JVM shutdown event in this way: 您可以尝试以这种方式捕获JVM关闭事件:

    Runtime.getRuntime().addShutdownHook(new Thread() {

        public void run() {
            System.out.println("BYE BYE");
        }
    });

The other option is to implement ServletContextListener by using @WebListener Annotation. 另一种选择是使用@WebListener Annotation实现ServletContextListener。 No xml configuration is required in this case. 在这种情况下,不需要xml配置。

@WebListener
public class MyLifeCycleListener implements ServletContextListener {

      public void contextInitialized(ServletContextEvent event) {
          //TODO ON START
      }

      public void contextDestroyed(ServletContextEvent event) {
          //TODO ON DESTROY
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM