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

2019-05-21 03:21发布

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?

2条回答
老娘就宠你
2楼-- · 2019-05-21 03:59

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

查看更多
时光不老,我们不散
3楼-- · 2019-05-21 04:18

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.

查看更多
登录 后发表回答