0

I'm developing a file upload service in Java which watches a selected local folder for changes (new files added, files modified, etc) and uploads every new file to a cloud storage. My problem is, that the WatchDir service I'm using (for detecting any change in the folder) needs to call the "upload files" method when changes are detected, BUT this method doesn't have the time to finish the upload until is called again by the WatchDir service.

I need to use some kind of execution queue for creating a queue with the number of calls the WatchDir service initiates on the file upload function, and this execution queue should wait for every instance of upload function to finish or throw an error and move on to the next iteration of calling the same function, until the queue is empty. This should work again if a new call on the upload file function gets in the execution queue, and shouldn't influence or delay the WatchDir service in any way.

I'm using this WatchDir service and works very well for my needs: https://docs.oracle.com/javase/tutorial/essential/io/notification.html

Any help or piece of code example would be much appreciated!

Aaron Marton
  • 131
  • 1
  • 5
  • 1
    The watch service has some queuing built in; you have a bit of time to respond before it starts dropping notifications. You can also detect when notifications are missed, which could help you to test whether your handler is generally fast enough. You could also use it to implement a fallback strategy where you re-read the current state of the directory if you lose track. But, if you really need a queue, you can just submit tasks to an `ExecutorService` configured with an unbounded queue. – erickson Aug 20 '16 at 18:50
  • This was an easy to implement useful tip and works fine. Thank you! – Aaron Marton Aug 22 '16 at 15:12

0 Answers0