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?

sbook
  • 841
  • 1
  • 8
  • 13

2 Answers2

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);
makc
  • 1,050
  • 1
  • 9
  • 20
  • 1
    Better note that StandardWatchEventKinds enum is in com.sun.* packages. – eskatos Sep 16 '13 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: https://gist.github.com/DinisCruz-Dev/9214909 – Dinis Cruz Feb 25 '14 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. – Vincenzo Pii Jun 28 '14 at 20:31
  • This improved my delay on OS X from 10 seconds to 2 seconds, which is unfortunately not enough for my app. Maybe I'll need to write a custom poller (50-100ms delay would be adequate). – Esko Luontola Sep 05 '14 at 22:08
  • 3
    `StandardWatchEventKinds` is now part of the `java.nio.file` package, though `SensitivityWatchEventModifier` is still under a Sun private package. – asgs Jun 12 '15 at 06:34
  • `import com.sun.nio.file.SensitivityWatchEventModifier;` – Paul Jun 22 '15 at 07:18
  • 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 02 '15 at 06:04
  • @asgs the question was about os x, though ) – makc Jul 02 '15 at 09:58
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.

karlgold
  • 7,970
  • 2
  • 29
  • 22
  • 8
    Is this still the case in JDK 8 for MacOS? – Ben McCann Apr 29 '14 at 14:48
  • 2
    @ben, apparently yes. We are still having issues due to it being very slow and not picking all events. – Johnny Everson Dec 24 '14 at 18:42
  • 3
    It looks like this won't be solved for JDK 9 either. The issue is still open https://bugs.openjdk.java.net/browse/JDK-7133447 and there wasn't a resolution in the past mailing list discussion: http://mail.openjdk.java.net/pipermail/nio-dev/2014-August/002691.html . – Lari Hotari Apr 09 '15 at 21:19
  • 1
    I posted a question about JDK-7133447 status on nio-dev mailing list: http://markmail.org/message/p6zd4svwqo32nu57 – Lari Hotari Apr 11 '15 at 14:44
  • 1
    @ben Yes this is still a problem with Java8 :-(. I wrote a Map implementation based on the WatcherService and found a massive difference between Mac OSX and Linux Ubuntu. To test for yourself just call some methods on the FileSystemMap and register for the events produced to see the difference. (http://www.rationaljava.com/2015/04/filesystemmap-natural-way-to-interact.html) – Dan Apr 27 '15 at 15:43
  • 1
    I have used BarbaryWatchService on Mac OSX and it works great. I highly recommend it. For me java.nio.file.WatchService is slow. This is with the latest Java 8 in mid-2015. – Robert Tupelo-Schneck Jul 22 '15 at 15:12
  • 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](https://github.com/gjoseph/BarbaryWatchService/issues/10) on the project. – Justin Nov 20 '17 at 11:48
  • 4
    Java 13 now, any changes? – mjs Dec 11 '19 at 15:08
  • I'm trying this: https://github.com/gmethvin/directory-watcher – Chris H. Feb 05 '20 at 22:14