I have working code that listens to a directory using the WatchService and responds to the events I specify. This works fine and has been tested on both linux and mac (although on the latter it's clear that polling is used).
However, when I deployed this in production it turns out the directory being monitored is an NFS mount. Since the WatchService uses inotify when running on linux there were never any events triggered because NFS mounts don't trigger inotify events (or something like this, there's more info here, which explains my problem: Java WatchService not generating events while watching mapped drives).
Since my code is already written I'd prefer to force the WatchService to use the polling implementation rather than the inotify one. Is there a way to do this?
I attempted this by finding the sun.nio.fs.PollingWatchService source code and creating an object directly (instead of using FileSystems.getDefault().newWatchService()) but when registering the service with the Path I got this exception: java.nio.file.ProviderMismatchException.
So, any ideas? Since I've already implemented the code using the WatchService and WatchKey API it'd be a lot easier to just force polling than rewrite everything using a custom or 3rd-party poller. Thanks!
File f = new File(YOUR_DIRECTORY, "_garbage.tmp"); new FileOutputStream(f).close(); f.delete();
and just listen for the creation of files named"_garbage.tmp"
? – Fils