简体   繁体   English

在servlet的doGet()仍在运行时检测J2EE容器停止事件

[英]Detect J2EE container stop event while a servlet's doGet() is still running

I am trying to detect an application stop event (in Websphere 8.0) to invoke a servlet's static method. 我正在尝试检测应用程序停止事件(在Websphere 8.0中)以调用Servlet的静态方法。 I wonder if there's a straightforward way to find out if the application has been ordered to stop. 我想知道是否有一种直接的方法来找出应用程序是否已被命令停止。

This is the scenario: 这是方案:

  1. I have one servlet, WorkerServlet , running a loop like this while(!forceStop){...} in its doGet(). 我有一个servlet, WorkerServlet ,在其doGet()中运行了while(!forceStop){...}这样的循环。 While the application is up, this loop is continuously running. 当应用程序启动时,此循环将连续运行。
  2. When the container stops the application (because of any event such as republish, stop, restart the application/server) I would like to invoke the static method WorkerServlet.forceStop(){forceStop = true;} . 当容器停止应用程序时(由于任何事件,例如重新发布,停止,重新启动应用程序/服务器),我想调用静态方法WorkerServlet.forceStop(){forceStop = true;}

This way, WorkerServlet's loop would finish and the servlet could be destroyed by the container right away. 这样,WorkerServlet的循环将结束,并且Servlet可能会立即被容器破坏。 The current behaviour is that the container waits for 60 seconds before forcing the destroy which is unacceptable during development time. 当前的行为是容器在强制销毁之前等待60秒,这在开发期间是不可接受的。

I have tried different approaches but none of them works 我尝试了不同的方法,但没有一个有效

  1. A ServletContextListener.contextDestroyed() . ServletContextListener.contextDestroyed() Not called until all servlets are destroyed 在所有servlet被销毁之前不调用
  2. A Spring ApplicationListener to detect when de application context is going to be destroyed. 一个Spring ApplicationListener,用于检测何时将销毁应用程序上下文。 Not called until all servlets are destroyed. 直到所有servlet被销毁才调用。
  3. Another servlet with a destroy() method that invokes WorkerServlet.forceStop() . 另一个带有destroy()方法的servlet,它调用WorkerServlet.forceStop() The container doesn't destroy() this servlet since there's another one running a request. 容器不会破坏该servlet,因为还有另一个正在运行请求。

So, is there any other way to detect the container's app shutdown event so that I can invoke that static method? 因此,还有其他方法可以检测容器的应用程序关闭事件,以便我可以调用该静态方法吗?

The only workaround I found was to use a startup bean. 我发现的唯一解决方法是使用启动Bean。 Fortunately Webshpere's EJB container is stopped before the servlet container: 幸运的是,Webshpere的EJB容器在servlet容器之前停止了:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class WorkerServletStopper {

    @PreDestroy
    public void preDestroy(){
        WorkerServlet.forceStop();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM