I have a long running timer task. Its a batch process actually that will keep looping forever and working its stuff. This batch program is written in Java. Now how do I ask it to gracefully shutdown if for example I need to do some maintenance? From outside the JVM?
I could think of some dirty ways, at the end of every loop of the run method, have it check for the presence of a file in some directory. If it is present then it will stop. Or create a database record asking it to stop.
Is there another better way of doing this?
For a graceful shutdown the service will have to check something periodically. It could be end (or beginning) of every loop or less frequent. So I think your approach is correct.
Checking for the presence of a file can be good way to do it. Having to delete the file could get tedious — one option is for the method itself to delete the file before exiting.
Other option is to run some socket listener as part of your application. There may be security issues though.