J. Lewis Muir > Software > Pollingwatchservice

Pollingwatchservice is a pure Java library providing a watch service (i.e. a WatchService implementation) that polls for changes. In other words, rather than depending on the operating system's native event notification system (if one exists), Pollingwatchservice explicitly detects changes only through periodically polling the file system and observing the difference between how the file system appeared then and how it appeared in a previous poll.

One use for this library is where the underlying operating system supports a native file system event notification system (e.g. inotify in Linux), and so the JVM produces a WatchService that uses it, but the native event notification system does not work on all file systems on the machine. If we attempt to watch a directory on a file system for which the native file system event notification system does not work (e.g. Lustre), we will see that the WatchService does not work. By using Pollingwatchservice, we can observe changes where we could not with the WatchService provided by the JVM.

Example:

PathWatchService s = new PollingWatchService(4, 15, TimeUnit.SECONDS);
s.register(FileSystems.getDefault().getPath("/home/luke"),
    StandardWatchEventKinds.ENTRY_CREATE,
    StandardWatchEventKinds.ENTRY_DELETE,
    StandardWatchEventKinds.ENTRY_MODIFY);
s.start();
for (;;) {
  WatchKey k = s.take();
  ...
}

Requirements

License

Release Notes

Download