I have a problem with WatchService. Here is a snippet of my code:
public void watch(){ //define a folder root Path myDir = Paths.get(rootDir+"InputFiles/"+dirName+"/request"); try { WatchService watcher = myDir.getFileSystem().newWatchService(); myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); WatchKey watckKey = watcher.take(); List<WatchEvent<?>> events = watckKey.pollEvents(); for (WatchEvent event : events) { //stuff } }catch(Exception e){} watckKey.reset(); }
* First of all, be aware that watch () is called inside an infinite loop.
The problem is that when creating multiple files at a time, some events are missing. For example, if I copy-paste three files into the "... / request" folder, only one falls into the trap, the rest remain as if nothing was happening, no OVERFLOW event was fired. On some different computers and OS, it reaches two files, but if you try 3 or more, the rest remain untouched.
I found a workaround, but I don't think this is the best practice. This is the thread:
The process begins and then stops at
WatchKey watckKey = watcher.take();
as expected (according to Event Handling ). Then I delete 3 files together in the "request" folder, so the process resumes with
List<WatchEvent<?>> events = watckKey.pollEvents();
The problem is here. It seems that the stream goes so fast through this line that two CREATED events are left behind and lost, only one is taken. The workaround was to add an extra line directly above this, for example:
Thread.sleep(1000); List<WatchEvent<?>> events = watckKey.pollEvents();
This seems to be a solution for at least three and several simultaneous files, but it does not scale at all. Therefore, in conclusion, I would like to know if there is a better solution to this problem. FYI, I am running Win 7 64
Many thanks!