Reputation: 42050
I have a Java web application, packaged as a "war" and deployed in Tomcat
. The application uses Jersey
to implement REST web services.
This application sometimes updates a text (XML) file. I guess that if Tomcat
is stopped while the application is still updating this file, the file may be corrupted. In order to prevent the file corruption I would like Tomcat
to wait until the file is closed before the exit. Now I wonder how to implement it.
So, my question is how to make Tomcat
wait until a web application finishes.
Upvotes: 0
Views: 870
Reputation: 4993
I'd take a different approach in your case (this may not be an answer to your question, but a proposal for your case).
Write the new xml file in a temporal one (conf.xml.tmp), so you still have the original one (conf.xml) untouched.
Once finished generating the xml, just move the newly generated one (conf.xml.tmp) on top off the original(conf.xml).
(You can also make a copy of the original for backup purposes).
This way, if tomcat stops while processing the xml generation you will always have a working xml.
The key here is that moving a file in the same disk is somehow an "atomic" operation, it will be done or not, but it will be not done half done.
Hope it helps
Upvotes: 2
Reputation: 10677
You can modify your shutdown script to take care of this. Make sure that file is in consistent state and then call shutdown.
You can check if file write is under process then sleep the script for say 5 min and then call shutdown.sh/shutdown.bat script.
Apart from this you can also do at java level; but i will prefer controlling it externally. This way you can modify it independent of your core application.
Upvotes: 1
Reputation: 16809
Have a look at the ServletContextListener is particular the context Destroyed method.
Notification that the servlet context is about to be shut down.
void contextDestroyed(ServletContextEvent sce)
Upvotes: 2