7

I just saw an awesome feature with java 7, the directory watcher. It'll tell you when something changed in a directory without polling the directory.

1.) But it says it falls back to polling if the filesystem doesn't support registering for change events. Do all typical linux and windows filesystems(extX,ntfs,reiserXXX,jsf,zfs) support this feature?

2.) Is renaming a file inside a directory a create or a change event? Or is that one delete and one create? I can test it on one system, but will it then be the same for all filesystems?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

2 Answers2

2

It looks like you're talking about the WatchService.

The wording of the ENTRY_CREATE event says that a new entry would be perceived if a new file is created or a file is renamed into the directory. It lacks specification of what events are fired if a file is renamed and remains in the same directory.

The wording also states that whether the service is based on the operating system or polling is implementation dependent. I suspect that's implementation by the JRE, so even if you know a particular OS supports it, it's not a guarantee that the service will use the OS-level functionality or resort to polling. In fact, the service does not provide any way to tell whether it is using polling or an OS-level feature at all.

The operations the API define also do not behave like a Listener. The WatchService does do automatic watching, but to acquire the list of events which happen, you still have to manually request the seen events from the service. It doesn't appear to provide any hooks to automatically get called when a new event is present.

Atreys
  • 3,741
  • 1
  • 17
  • 27
  • okay, but I think the JRE you can download from oracle will lead the way for windows and linux. That's all I'm really interested about. But still the OS and Filesystem must have some features before this works. Any ideas who the oracle jre handles that? – Franz Kafka Jun 11 '11 at 11:49
  • 1
    [inotify](http://en.wikipedia.org/wiki/Inotify) is a linux tool that profides notification when a file changes. Windows has a service like this too: [FindFirstChangeNotification](http://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx) is available in .NET. I don't know how the oracle jre handles it, but I imagine it redirects to one of those via jni if they are available. – Atreys Jun 11 '11 at 12:00
1

If you play with it on Windows and on Linux, you'll see lots of differences in behavior. So Java does not really attempt to provide a consistent platform independent abstraction. You have to test your app on the OSes you care about (well, there are only 2).

WatchService sucks more than you can imagine. Prepare for frustrations if you really dive into it.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • Okay I will keep that in mind. It seems so efficient not to have to poll the directories all the time. Waited ages for such a feature, in 17 days it's there. And now it's crap!?! Hopefully it won't suck too hard :-) – Franz Kafka Jun 11 '11 at 20:17
  • 1
    ok I scared you too much. it's just more complicated than one would think initially. – irreputable Jun 11 '11 at 23:42