shutdown hook in tomcat (necessary) not running

99封情书 提交于 2019-12-06 05:02:58

问题


I have a long running cpu and database intensive thread (that runs at low priority) that can be fired off by my tomcat application by an administrator request.

I use a shutdown hook for this thread, so that if the application is stopped forcefully, it can clean up the database state before exiting. Works in standalone mode perfectly, with control-C and canceling in eclipse console.

But when running under tomcat, and when I do a:

$CATALINA_HOME/bin/shutdown.sh

I verified with debug logging that my servlet contextDestroyed message gets sent by tomcat, but for this thread, there is no way that message can stop it, and the important point is that my shutdown hook never fires.

I noticed other people saying use contextDestroyed message, but that won't work for me, and also some questions and answers imply that the shutdown hook works for them in tomcat.

I am using tomcat 6.0.35.


回答1:


I have a long running cpu and database intensive thread (that runs at low priority) that can be fired off by my tomcat application by an administrator request.

Why not run this out-of-process and not bother with the webapp, context events, shutdown hooks, or any of that rot?

Shutdown hooks are a kludge, and as @StephenC says, they won't fire when you want them to.




回答2:


I noticed other people saying use contextDestroyed message, but that won't work for me

Well I think you need to think how you can make it work.

A webapp setting a shutdown hook is not the correct solution.

  • The shutdown hook won't run when your webapp is shutdown by the web container. This means you have a problem with orphaned DB threads to deal with if you restart or redeploy your webapp.

  • If and when your shutdown hook does eventually run, it is liable to fail because the rest of your webapp has already been shut down by the web container. For instance, the webapp's JDBC connection pool will typically have been nuked. Note that a lot of the "nuking" happens behind the scenes as part of Tomcat's strategy for avoiding PermGen storage leaks on redeploy ...



来源:https://stackoverflow.com/questions/11443133/shutdown-hook-in-tomcat-necessary-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!