如何捕捉关闭tomcat的事件? [英] how to catch the event of shutting down of tomcat?
本文介绍了如何捕捉关闭tomcat的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要的是每次tomcat服务器启动时都启动一个线程。为此我需要捕获关闭tomcat的事件。我可以这样做吗?我尝试使用会话来做但有时会话甚至关闭并重新启动tomcat后会持续存在吗?我的选择是什么?
What I want is to start a thread every time the tomcat server starts.For this I need to catch the event of shutting down of tomcat.How can I do this?I tried to do it using sessions but sometimes the session even persists after shutting down and restating tomcat?what are my options?
推荐答案
您可以尝试以这种方式捕获JVM关闭事件:
You can try to catch an JVM shutdown event in this way:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("BYE BYE");
}
});
另一个选项是使用 @WebListener 注释实现ServletContextListener。在这种情况下,不需要xml配置。
The other option is to implement ServletContextListener by using @WebListener Annotation. No xml configuration is required in this case.
@WebListener
public class MyLifeCycleListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
//TODO ON START
}
public void contextDestroyed(ServletContextEvent event) {
//TODO ON DESTROY
}
}
这篇关于如何捕捉关闭tomcat的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文