0

I have an executable jar file which runs up to process files in a source directory. When starting to process a file, it is moved into a "processing" directory and finally moved into a completed directory.

This executable jar file is run up via a cron job

The issue I have encountered is that the executable jar runs are overlapping ie one run has not completed and a second run has started.

So there are 2 or maybe more executable jar running at the same time to process the files from the same source directory.

The idea is once a file is moved into a "processing" directory by the executable jar run, the file will not be visible to another executable jar run in the source directory.

However, it appears 2 or more of the executable jar runs are gaining the file in the source directory and start processing the file. The consequence is that only one executable jar run is able to complete the processing of the file and the others that thought had exclusive access to the file, are failing because the file is no longer there.

I am using the Files.move( srcPath, targetPath) provided in the Files class which is in the java.nio.file package to move files between directory.

Any suggestions as to how I could ensure only one executable jar run process a file at a time please ?

Thank you

Pete

Pete Long
  • 107
  • 2
  • 11
  • Would it be possible to ensure, that only one jar is running? So when a jar detects, that another is running, it just stops? You could create a lock file for that. – Rainer Schwarze Oct 20 '19 at 20:56
  • Simple and effective. Thinking of using `FileChannel channel = FileChannel.open(path); FileLock lock = channel.tryLock();`. . Is this a good approach or is there better ? Thank you – Pete Long Oct 20 '19 at 21:30

1 Answers1

2

For good examples of java.nio.channels.FileLock see How can I lock a file using java (if possible)

Exclusive-lock each srcPath before processing it.

More generally, as commented above, ensure there is only only instance of your program running. Create a specific lock-file that you hold exclusive access on from start to finish. Use tryLock() and quit if you cannot acquire it (since that would mean another instance already has it).

drekbour
  • 2,895
  • 18
  • 28
  • Sorry a bit late with the response .. had to get some sleep. Is it possible to get an exclusive lock on a folder if so, could you provide an example please. I ask because the executable jar runs are on different servers but are accessing a shared source folder to process the files. If there is such a mechanism, the solution would mean, each executable jar run would attempt to lock on the source folder. If it doesn't aborts, otherwise process the files. This way ensures only ONE executable jar run is actually processing the files at any one time. Thank you again – Pete Long Oct 21 '19 at 08:50
  • This doesn't sound like a Java problem any more. You have two hosts sharing some file system. It's entirely dependent on the technologies involved as to whether you can lock that FS at all. Last basic option is to make targetPath different for each process. Both might try to move file A from common-src/A -> processing-/A but only one can succeed. – drekbour Oct 21 '19 at 21:30