简体   繁体   中英

Java shutdown function on jar unloading

Is there a mechanism in Java that would allow me to imbue a function or runnable into something-or-other that will be called if the jar is about to get unloaded ?

Note that the JVM will, in general, stay up. That rules out Runtime.addShutdownHook .

(It's required to facilitate hot redeployment to a web server).

There's nothing in Java SE, as far as I'm aware.

But since you mention "web server", you might be looking at a Java EE solution. And then there's ServletContextListener . Just implement that interface and configure the implementing class in your web.xml .

For a pure Java SE solution, I would not bind the library-loading to a class, but to an instance instead. This way you can have tighter control over when it is loaded and unloaded.

You could even use finalize here, as that's one of the few cases where it would be appropriate. This would have the drawback of being non-deterministic (ie you can't depend on the object being finalized when the new version is being loaded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM