How do I know if a shutdown is in progress?

In Tomcat, when the server closes, it tries to set all the static variables in the classes that it knows about to be equal to zero. Touching these classes, their static initializers are launched, which can lead to an endless loop for some of our deprecated classes that have massive static initializers.

Is there an elegant way to determine if a shutdown is complete? Then we could check at the top of the static initializers if we are in shutdown mode and then ignore the initialization.

The only way we work with is nothing but elegant:

try{ Thread hook = new Thread(); Runtime.getRuntime().addShutdownHook( hook ); // fires "java.lang.IllegalStateException: Shutdown in progress" if currently in shutdown Runtime.getRuntime().removeShutdownHook( hook ); }catch(Throwable th){ throw new Error("Init in shutdown thread", th ); } 
+7
source share
2 answers

You can see if Tomcat uses the new Thread to shut down, if so, you can cross the current threads and look for that thread.

Then by setting the ServletContextListener to turn the flag on and off in the context of Initialized and contextDestroyed, your classes check this flag.

+2
source

Do you have control over your webapp configuration? If so, you can disable static link cleanup by setting

 clearReferencesStatic=false 

in /META-INF/context.xml as indicated in the documentation

Otherwise, I agree that setting a flag in the ServletContextListener is probably your best option.

+2
source

All Articles