Tutorial :How do I know when a Java EE app is undeployed?



Question:

I need to close some streams when my Java EE app is undeployed to avoid pages of exceptions in the server.log every time I undeploy. The Exceptions say: "Input stream has been finalized or forced closed without being explicitly closed". I know how to close (with the close() method), but I dont know when.

finallize() comes too late, @PreDestroy is called not only before undeploying but also in normal operation mode. How does the app know, when it is undeployed?

Edit: I call this during creation:

    decisionLogger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(      ksession,       config.getString("rules.logfilename"),       config.getInt("rules.loginterval"));  

decisionLogger is of type KnowledgeRuntimeLogger which has only the close() method. No getter for the thread that works under the hood. To shut it down gently, I need to call the close method, but when?


Solution:1

Add a finalize() method to the logger or whichever class is causing the issues. Close the streams from that. Unless you exit with System.exit() it will run. If System.exit is being called you can use Runtime.addShutdownHook


Solution:2

There are no hard and fast answers because Java EE encompasses a lot of technologies with variying lifecycles.

But I'm going to take a stab in the dark and suggest you look at ServletContextListener. This will tell you when your web application is being stopped, which is probably the time you want to release resources - not on application uninstall.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »