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

久未见 提交于 2019-12-22 13:08:13

问题


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?


回答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




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



来源:https://stackoverflow.com/questions/3316300/how-do-i-know-when-a-java-ee-app-is-undeployed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!