NIO Watch Service Feedback
Alan Bateman
Alan.Bateman at Sun.COM
Thu Nov 12 09:29:50 PST 2009
Robert MacGrogan wrote:
> First of all, I want to say how happy I am to have support for file
> system events in Java. And the sample WatchDir program provided with
> the early JDK 7 distribution was a huge help to me in understanding
> how this new API works.
Good to know that this feature is useful and that the samples are worth
while.
>
> That said, I have a couple of comments.
>
> First of all, I believe I found a bug. If you rename a file or
> directory inside of a watched directory, a Create event will be
> generated for the new file/directory name. No Delete or Modify event
> will be created. It seems to me that it would be better to either have
> a rename generate a Modify event or a Delete on the old Path followed
> by a Create on the new Path.
Are you sure you aren't seeing a DELETE event? Can you say which
operating system (and version) this is? Also, can you duplicate this
with WatchDir?
>
> Second, the Watch Service API is fairly tricky to use. To implement
> anything with it you are essentially required to spawn a thread to
> listen for events (unless I'm really missing something). It seems to
> me that there is a need for an additional layer of abstraction on top
> of the current API, something following the Listener pattern.
> Something like this would be nice:
>
> Path.addPathEventListener(new PathEventAdapter() {
> public void childFileModified(WatchEvent<Path> event){
> System.out.println("File modified.");
> }
> });
>
The file change notification API is deliberately a low-level API as it
is relatively niche and must serve multiple masters. In the server case
it might be used in conjunction with a thread pool. In a client
application then you could do something along the lines that you
propose. We don't have any plans to add another layer to this but we
could include an additional sample that demonstrates how this can be
done - in its simplest form it would take a key, dispatch each event to
the appropriate listener method, reset the key, and go back to waiting.
-Alan.
More information about the nio-dev
mailing list