[英]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: 这是方案:
WorkerServlet
, running a loop like this while(!forceStop){...}
in its doGet(). WorkerServlet
,在其doGet()中运行了while(!forceStop){...}
这样的循环。 While the application is up, this loop is continuously running. 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 我尝试了不同的方法,但没有一个有效
WorkerServlet.forceStop()
. WorkerServlet.forceStop()
。 The container doesn't destroy() this servlet since there's another one running a request. 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.