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!