0

I am working on a simple Spring console application (it is a litle batch working on a database).

This batch is scheduled to start at specific time. The problem is that the execution of this program can take a lot of time and the scheduler can try to start it before the previous instance of this program completed its work.

So I think that I can use a lock file to handle this situation, my idea is:

  • When the batch start it check if there is this lock file in its folder (if this lock file is present, the batch application ends without doing anything).
  • If the lock file can't be found: create the lock file and start the batch operations.

Do you think that it could be a valid approach to the problem? Or Spring provide some smarter way?

Another doubt is: what is the standard for a lock file? Can I create simply a .lock file in the current folder?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    There should be attempt to recreate the file on start to prevent case when your app crashes closing JVM without the file removing. There is alternative approach to use Socket based lock but it also is not ideal. Could conflict with another applications using the same port. – StanislavL Apr 28 '17 at 14:53
  • 1
    http://stackoverflow.com/questions/920386/how-to-allow-running-only-one-instance-of-a-java-program-at-a-time – StanislavL Apr 28 '17 at 15:01

0 Answers0