Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Operating Systems support native (inotify-like) file watching in Java

The JavaDoc for java.nio.file.WatchService states;

The implementation ... is intended to map directly on to the native file event notification facility where available, or to use a primitive mechanism, such as polling, when a native facility is not available.

I assume this means it will try a lightweight, native mechanism when it can (something like inotify on Linux) and poll if it can not.

Is that correct?

What operating systems are likely or unlikely to provide such a facility? Distro level for Linux would be really useful or can I assume that if the JVM runs on *unix, it will be supported?

like image 833
Toby Avatar asked Mar 31 '17 13:03

Toby


People also ask

How inotify works in Linux?

How does Inotify Work? The Inotify develops a mechanism for monitoring file system events, which watches individual files & directories. While monitoring directory, it will return events for that directory as well as for files inside the directory.

What is inotify watch?

Inotify Watch helps to keep track of the file changes under the directories on “watch” and report back to the application in a standard format using the API calls. We can monitor multiple file events under the watched directory using the API calls.

Which syscall or feature within the Linux kernel is used to monitor changes in multiple files simultaneously?

13 kernel, Linux has included inotify, which allows a monitoring program to open a single file descriptor and watch one or more files or directories for a specified set of events, such as open, close, move/rename, delete, create or change attributes.


2 Answers

This should have probably been a comment, but it's too big to post it as such...

I am looking at jdk-9 sources (could easily be searched in the jdk-8 repo as well), but here is some relevant to your question comments:

   /**
     * Linux implementation of WatchService based on inotify.
     *
     * In summary a background thread polls inotify plus a socket used for the wakeup
     * mechanism. Requests to add or remove a watch, or close the watch service,
     * cause the thread to wakeup and process the request. Events are processed
     * by the thread which causes it to signal/queue the corresponding watch keys.
     */

 class LinuxWatchService  extends AbstractWatchService

And for windows:

/*
 * Win32 implementation of WatchService based on ReadDirectoryChangesW.
 */

class WindowsWatchService extends AbstractWatchService

And so on.. you can find all the available implementations under:

 jdk/src/java.base/{windows|unix|solaris|linux...}/classes/sun/nio/fs/

As what OS actually supports this, it seems like that would require for you to look at the actual distro.

like image 89
Eugene Avatar answered Sep 25 '22 05:09

Eugene


I think that you can find some information in this post Watchservice in windows 7 does not work. There is some note about file system watchers on Windows and MacOSX (author of post says that they are using polling). On Linux file system monitoring is part of a Linux kernel and it's implemented using quite nice notifications mechanism (more info about some of them can be found here)

like image 31
Kamil Avatar answered Sep 21 '22 05:09

Kamil