I have come across the shutdown hooks functionality which is invoked when java application is shutdown.
I am using SIGTERM signal to shutdown my java process on a linux Box.
And at the time of shutdown, I want to do some disk I/O (persisting some objects) and closing open file buffer( that can take upto 1min)
Can you advise if shutdown hooks are a safe/reliable mechanism for this use case
If there is some better alternative, please suggest.
This design document does not say anything about how long a shutdown hook thread is allowed to run. But reading between the lines (the fact that there is a Runtime.halt
, I think you are safe.)
Now keep in mind that some other frameworks you rely on may have been shutting down concurrently, so your hook should not rely on anything other than your own code.
You may also find this other SO question of interest: How to stop java process gracefully?.