我如何知道我的tomcat Web应用程序何时停止?

时间:2021-04-02 20:13:58

I want to get an 'event' before tomcat stops my web application.
My application needs to "wrap up" stuff before it is being closed.

我想在tomcat停止我的web应用程序之前得到一个'事件'。我的应用程序需要在关闭之前“收拾”它们。

3 个解决方案

#1


A servlet that does nothing but respond to destroy() is functional but a bit of a hack. Servlets are meant to handle web requests.

除了响应destroy()之外什么都不做的servlet是功能性的,但有点像黑客攻击。 Servlet用于处理Web请求。

The proper thing to do is to implement a ServletContextListener and do your wrap up stuff in the contextDestroyed() method.

正确的做法是实现一个ServletContextListener并在contextDestroyed()方法中包装东西。

Register your ServletContextListener in web.xml like:

在web.xml中注册您的ServletContextListener,如:

<listener>
    <listener-class>com.foo.MyListener</listener-class>
</listener>

#2


I think I found it, I created a servlet and implemented the

我想我找到了它,我创建了一个servlet并实现了

public void destroy();

I verified that this method is invoked when I stop the application from the tomcat admin page, and even when I shut-down tomcat server

我验证了当我从tomcat管理页面停止应用程序时调用此方法,甚至当我关闭tomcat服务器时

JavaEE 6 doc on Servlets

关于Servlets的JavaEE 6 doc

#3


You can write a ContextListener that responds to deploy/undeploy events. Maybe you can have it sends an email when the context is undeployed.

您可以编写一个ContextListener来响应deploy / undeploy事件。也许你可以让它在取消部署上下文时发送电子邮件。

I'm just not sure if that event is fired if the app server is stopped.

我不确定如果应用服务器停止,是否会触发该事件。

#1


A servlet that does nothing but respond to destroy() is functional but a bit of a hack. Servlets are meant to handle web requests.

除了响应destroy()之外什么都不做的servlet是功能性的,但有点像黑客攻击。 Servlet用于处理Web请求。

The proper thing to do is to implement a ServletContextListener and do your wrap up stuff in the contextDestroyed() method.

正确的做法是实现一个ServletContextListener并在contextDestroyed()方法中包装东西。

Register your ServletContextListener in web.xml like:

在web.xml中注册您的ServletContextListener,如:

<listener>
    <listener-class>com.foo.MyListener</listener-class>
</listener>

#2


I think I found it, I created a servlet and implemented the

我想我找到了它,我创建了一个servlet并实现了

public void destroy();

I verified that this method is invoked when I stop the application from the tomcat admin page, and even when I shut-down tomcat server

我验证了当我从tomcat管理页面停止应用程序时调用此方法,甚至当我关闭tomcat服务器时

JavaEE 6 doc on Servlets

关于Servlets的JavaEE 6 doc

#3


You can write a ContextListener that responds to deploy/undeploy events. Maybe you can have it sends an email when the context is undeployed.

您可以编写一个ContextListener来响应deploy / undeploy事件。也许你可以让它在取消部署上下文时发送电子邮件。

I'm just not sure if that event is fired if the app server is stopped.

我不确定如果应用服务器停止,是否会触发该事件。