从 Java Web 应用程序取消部署时调用方法 [英] Call method on undeploy from a Java web-application
问题描述
我正在开发一个 Java 网络应用程序.应用程序连接到 Lucene 索引.我创建了一个 IndexSearcher 的单例实例.此实例打开一些文件.当我重新部署 Web 应用程序时,由 IndexSearcher 的较早实例打开的文件继续保持打开状态,并且重新部署的应用程序会创建另一个实例.几次重新部署后,系统开始抛出打开的文件太多"异常.我想在重新部署之前关闭旧实例,以便关闭旧文件,但我不知道该怎么做?web.xml 中是否有一个指令在 un-deploy 时调用,类似于 load-on-startup?我在 jboss 服务器上运行 web 应用程序.
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 redeploy the web-application, the files opened by the earlier instance of IndexSearcher continue to remain open, and another instance is created by the redeployed application. After a few redeploys, the system starts throwing a "too many open files" exception. I would like to close the old instance before redeploying, so that the old files are closed, but I cannot figure out how to do that? Is there a directive in web.xml that's called upon un-deploy, similar to load-on-startup? I'm running the web-application on a jboss server.
推荐答案
@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.
}
}
如果您还没有使用 Servlet 3.0(尽管它已经推出 2 年了),那么您需要删除 @WebListener
注释并在 web.xml 中手动注册它
如下:
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>
这篇关于从 Java Web 应用程序取消部署时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!