53

WatchService looks like a great technology but its been too slow to be useful on the OS X and Linux systems I've tested on. To add insult to injury, it doesn't seem to get notified of all events either.

This is the case both with my own code and the canonical example from Oracle. (http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java)

I recognize that the OS X OpenJDK port is unsure of this functionality (see https://wikis.oracle.com/display/OpenJDK/Mac+OS+X+Port+Project+Status)

Has anyone been using this in production with success?

2
  • 5
    It's a very annoying known issue since 2012.
    – andruso
    May 12, 2015 at 21:24
  • 4
    The question indicates "OS X and Linux" but the answers and comments seem to indicate this is a problem only on OS X. Jul 22, 2015 at 17:45

2 Answers 2

45

I have much better response times if I change

folder.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);

to

folder.register(watcher, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH);
8
  • 1
    Better note that StandardWatchEventKinds enum is in com.sun.* packages.
    – eskatos
    Sep 16, 2013 at 17:52
  • YES! that is what made it for me, I was having 4 to 5 secs delay until I used that. For reference the script I created is here: gist.github.com/DinisCruz-Dev/9214909
    – Dinis Cruz
    Feb 25, 2014 at 18:44
  • Same for me, I had a delay of at least 4 seconds and this made it work like it (normally) works on Linux or Windows. Thanks a lot. Jun 28, 2014 at 20:31
  • 3
    StandardWatchEventKinds is now part of the java.nio.file package, though SensitivityWatchEventModifier is still under a Sun private package.
    – asgs
    Jun 12, 2015 at 6:34
  • 2
    @Paul sure, but since it's a private package, it's not portable across the different JVM implementations (like IBM's for example).
    – asgs
    Jul 2, 2015 at 6:04
32

JDK 7 does not yet have a native implementation of WatchService for MacOS. Rather than listening for native file system events, it uses the fallback sun.nio.fs.PollingWatchService, which periodically traverses the file system and checks the last modified timestamp of each file and subdirectory in the tree. I've also found it to be unusably slow.

There is a native implementation of WatchService for Mac:

http://code.google.com/p/barbarywatchservice/

I haven't tried to use it myself.

9
  • 8
    Is this still the case in JDK 8 for MacOS?
    – Ben McCann
    Apr 29, 2014 at 14:48
  • 2
    @ben, apparently yes. We are still having issues due to it being very slow and not picking all events. Dec 24, 2014 at 18:42
  • 3
    It looks like this won't be solved for JDK 9 either. The issue is still open bugs.openjdk.java.net/browse/JDK-7133447 and there wasn't a resolution in the past mailing list discussion: mail.openjdk.java.net/pipermail/nio-dev/2014-August/002691.html . Apr 9, 2015 at 21:19
  • 2
    Warning! We'd like to use BarbaryWatchService but have discovered it doesn't behave as expected with APFS on HighSierra. I've raised an issue on the project.
    – Justin
    Nov 20, 2017 at 11:48
  • 4
    Java 13 now, any changes?
    – mjs
    Dec 11, 2019 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.