javawatchservicenio2

Java 7 Watch Service cannot detect events properly


I have created a watch service java application using nio2's WatchService for a Linux server whereby I have it watching some directories in a mounted NAS drive. The files will arrive by NFS, MQ or SFTP.

I have read in other places stating that the WatchService will not be able to work on remote drives however it seems that it does work when I test it.

My tests are a mix of copying or 'touching' new files into the watched directory as the user that I've used to run the java service.

The problem: My WatchService is not detecting ANY events in those specific directories during live deployment. Upon further inspection, the files are owned by a different user/group than the one used to execute my java program.

As such I have performed a test whereby I had created a new user from a new group and then touched/copied into the directory and it works! The permissions of ALL the files are at least read for EVERYONE. (and even testing with no read for everyone, my program will at least log the CREATE_EVENT regardless)

I'm not able to procure access for the users that were used for NFS/MQ/SFTP to test out this problem. So I'm looking for some help or at least clarity on the issue that I'm facing here.


Solution

  • A Java watch service often cannot detect events on a remote mounted file system. This is what the javadoc says:

    If a watched file is not located on a local storage device then it is implementation specific if changes to the file can be detected. In particular, it is not required that changes to files carried out on remote systems be detected.

    Why? Because the underlying OS functionality that Java is using to implement the watch service can't do this either1.

    Why? Because the remote file system protocols don't support this. Certainly NFS doesn't. And neither does SMB.

    Why? Because it wouldn't be reliable, it wouldn't be efficient, and it wouldn't scale!

    If you really need to implement file watching for remote file systems, you will need to do it on the system where the files actually reside.


    1 - It is a bad idea to specify behavior that cannot be implemented!