Call method on undeploy from a Java web-application

后端 未结 2 536
长发绾君心
长发绾君心 2020-12-11 02:42

I am developing a Java web-application. The application connects to a Lucene index. I create a singleton instance of IndexSearcher. This instance opens some files. When I re

相关标签:
2条回答
  • 2020-12-11 02:52

    If you implement javax.servlet.ServletContextListener and register that class in web.xml as a <listener>, then then contextDestroyed() method will be called before the context is unloaded.

    0 讨论(0)
  • 2020-12-11 03:03

    Implement a ServletContextListener.

    @WebListener
    public class LuceneConfig implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            // Do your job here during webapp startup.
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // Do your job here during webapp shutdown.
        }
    
    }
    

    If you're not on Servlet 3.0 yet (which is already out for 2 years though), then you need to remove the @WebListener annotation and register it manually in web.xml as follows:

    <listener>
        <listener-class>com.example.LuceneConfig</listener-class>
    </listener>
    
    0 讨论(0)
提交回复
热议问题