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.
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.