Java 何时调用 contextDestroyed?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24628744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:33:25  来源:igfitidea点击:

When is contextDestroyed called?

javatomcatservletcontextlistener

提问by OldCurmudgeon

Having implemented a ContextListenerI can now happily deal with contextDestroyedevents by closing down my connection pools and flushing my caches etc.

实现了一个ContextListener我现在可以contextDestroyed通过关闭我的连接池和刷新我的缓存等来愉快地处理事件。

I was surprised recently when contextDestroyedwas called at a time when my server was not being shut down - it seemed to be at some arbitrary time which I have not been able to track down.

最近,contextDestroyed当我的服务器没有关闭时被调用时,我感到很惊讶- 它似乎是在某个我无法追踪的任意时间。

Is there any defined event or set of circumstances that trigger contextDestroyed?

是否有任何定义的事件或一组情况触发contextDestroyed

Should I ensure that everything I do when contextDestroyedis called is reversible? Do I need to make all my pools survive a destroyed/initializedcycle? Was I wrong to assume I would only get a contextDestroyedwhen Tomcat was being shut down or my warwas being replaced?

我应该确保我在contextDestroyed被调用时所做的一切都是可逆的吗?我是否需要让我的所有池都能存活一个destroyed/initialized周期?我错误地假设我只会contextDestroyed在 Tomcat 被关闭或被war替换时得到一个?

采纳答案by glend

Through a series of trial and error testing I have found that contextDestroyed()is called when;

通过一系列的反复试验,我发现contextDestroyed()调用when;

  1. The server is .WARis being updated/removed.
  2. The server is shutdown due to administrator intervention.
  3. The server is shutdown due to a coding error. Something that would terminate a non-server application termination.
  1. .WAR正在更新/删除服务器。
  2. 由于管理员干预,服务器关闭。
  3. 由于编码错误,服务器关闭。会终止非服务器应用程序终止的东西。

If you are experiencing issue #3, as you are suggesting, I think the best possible course of action is to safely (be sure not to create an infinite loop) call contextInitialized()to ensure pools are recreated properly.

如果您遇到问题 #3,正如您所建议的那样,我认为最好的做法是安全地(确保不要创建无限循环)调用contextInitialized()以确保正确重新创建池。

回答by MrTux

See: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

请参阅:http: //docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

contextDestroyed(ServletContextEvent sce): Receives notification that the ServletContext is about to be shut down.

contextDestroyed(ServletContextEvent sce):接收 ServletContext 即将关闭的通知。

I.e., it gets called when a web application gets unloaded (e.g., you remove or replace a .war file from the web-apps folder OR unload it using the Tomcat server-manager).

即,它在 Web 应用程序卸载时被调用(例如,您从 web-apps 文件夹中删除或替换 .war 文件或使用 Tomcat 服务器管理器卸载它)。