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!
You can try the open source project jpoller. It implements a class named DirectoryPoller which periodically poll the contents of one or more directories. It is a periodic thread that looks for new files using the file last modification time. To download source you can visit http://jpoller.sourceforge.net/ Honestly, I did not used jpoller. I used JDK 7 WatcherService events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With