从Java Web应用程序取消部署的调用方法 [英] Call method on undeploy from a Java web-application
问题描述
我正在开发Java Web应用程序.该应用程序连接到Lucene索引.我创建了IndexSearcher的单例实例.此实例将打开一些文件.当我重新部署Web应用程序时,由IndexSearcher的较早实例打开的文件继续保持打开状态,并且由重新部署的应用程序创建了另一个实例.重新部署几次后,系统开始引发打开的文件太多"异常.我想在重新部署之前关闭旧实例,以便关闭旧文件,但是我不知道该怎么做?是否在web.xml中有一个在取消部署时被调用的指令,类似于启动时加载?我正在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屋!