contextDestroyed() vs addShutdownHook()
Asked Answered
A

5

7

I'm currently implementing ServletContextListener and using contextDestroyed() to run cleanup tasks on my web application before it shuts down. However, I've been reading about how Runtime.addShutdownHook(Thread) can be used for the same purpose.

Is there any difference between these two methods of running cleanup before undeployment? Which is preferable for a web application, in terms of functionality, efficiency, and maintainability?

Axletree answered 5/8, 2011 at 1:10 Comment(0)
T
8

I think the ServletContextListener is more appropriate for a web application, because you clean up resources for each and every session.

A shutdown hook is executed with the JVM is shut down. That would be when you stop your container, which is a one-time event.

Telesis answered 5/8, 2011 at 1:18 Comment(0)
A
5

The danger with using addShutdownHook() is that you will likely get a classloader leak which will become apparent when you redeploy you app multiple times.

Because the shutdown hook's class (either a Thread subclass or a Runnable implementation in your webapp) is coming from your webapp's classloader, even after your webapp is undeployed by the container, the shutdown hook will still be registered with the system. This means the entire webapp's classloader cannot be garbage collected.

I'd definitely recommend ServletContextListener.

Automat answered 5/8, 2011 at 1:54 Comment(0)
P
4

Many servlet containers support operations for dynamically dropping and/or reloading WARs without shutting down the JVM process. Thus if you if you write your cleanup routine as a ServletContextListener, it could potentially be run several times during the life of the container. (For example, if you modify and reload your WAR several times while the container process is still up.)

However, if you implement your cleanup using Runtime.addShutdownHook, it will run only once: when the JVM for the container as a whole is shut down.

ServletContextListener is probably the right answer for you, as it couples your cleanup routine with the life of the web application, as opposed to the life of the container process which is hosting it.

Phthalocyanine answered 5/8, 2011 at 1:27 Comment(0)
S
3

Why not do both? While ServletContextListener is more appropriate for a webapp, I find that during development the server is often stopped abruptly and then contextDestroyed() is never called, so you can have both mechanisms in place to ensure there is always a graceful shutdown:

Implement a ServletContextListener where contextInitialized calls addShutdownHook() and contextDestroyed calls removeShutdownHook(). The hook and contextDestroyed can both delegate to some internal method to actually do the cleanup.

This way, if the listener is invoked properly then the hook gets added and removed (but not invoked) and there is no leak, but if the server dies without the context being destroyed, the shutdown hook cleans things up.

Sidnee answered 22/2, 2017 at 21:55 Comment(0)
P
2

The leak which we are talking about will only happen if we do the hot/deployment. However, if server is restarted after every deployment change than hook should work fine without memory leak. Also, one more factor governing leak is type of resource cleaning you are trying to control via listener/hook.

Propylite answered 31/10, 2011 at 3:38 Comment(1)
This is a comment to a previous answer not an answer per se.Constitutionalism

© 2022 - 2024 — McMap. All rights reserved.