Java 7 NIO watchservice vs jpathwatch
Asked Answered
G

3

8

The project I'm working has been using Java 6 and jpathwatch (.95) and is now upgrading to Java 7. Currently on Windows 7 and 2008 Server. I'm refactoring areas of code to use the new Java 7 NIO and is relatively straight forward - even using the NIO.2 to replace jpathwatch. However, the file watching area of our code began failing unit tests. It seems the Java 7 NIO will not pick up changes in UNC paths to other machines -

\\otherMach\path\to\watch.  

To test, I implemented the code from the Java NIO tutorial site http://docs.oracle.com/javase/tutorial/essential/io/fileio.html and then created a duplicate class swapping in the jpathwwatch imports instead of the Java NIO imports. jpathwatch works for the UNC paths but Java NIO does not. It seems to register and even returns an initial event key for the location: (sample output)

INFO: Watching: \\otherMach\path\to\watch
DEBUG: Added: \\otherMach\path\to\watch
INFO: Got event key: sun.nio.fs.WindowsWatchService$WindowsWatchKey@1f26ecd2
INFO: event key for: \\otherMach\path\to\watch

but then never recognizes any further changes.

jpathwatch registers and reports directory and file events (although it doesn't report the initial event right after registering).

INFO: Watching: \\otherMach\path\to\watch
DEBUG: Added: \\otherMach\path\to\watch
INFO: Got event key: name.pachler.nio.file.impl.WindowsPathWatchService$WatchRecord@79a7bd3b
INFO: event key for: \\otherMach\path\to\watch
INFO: EVENT RECEIVED: ENTRY_CREATE file/dir created - \\otherMach\path\to\watch\New folder
INFO: Got event key: name.pachler.nio.file.impl.WindowsPathWatchService$WatchRecord@79a7bd3b
INFO: event key for: \\otherMach\path\to\watch
INFO: EVENT RECEIVED: ENTRY_CREATE file/dir created - \\otherMach\path\to\watch\New Text Document.txt

This is despite seeing on the jpathwatch discussion that networked watching is NOT supported - note response by Uwe Pachler refering to UNC paths - http://sourceforge.net/p/jpathwatch/discussion/888207/thread/8ea778de/?limit=25#0037

Has anyone had any luck with watching UNC paths and Java 7 NIO.2? Any other or more recent solutions?

Thank you,

-mjash

Guardroom answered 2/10, 2013 at 15:1 Comment(1)
Sadly I just came across the same issue and behavior with UNC and Java7 WatchService.Fonsie
F
2

It looks like this is a bug in the JDK which was fixed in JDK 1.7.0_u60. I just tried u71 (had been using u45) and verified it now works for me across a UNC.

Fonsie answered 29/10, 2014 at 20:53 Comment(0)
C
0

If you are using the tutorial and examples from the Oracle documentation on WatchEvent, you might have missed calling key.reset() after handing the event. I just ran into the same issue:

    void processEvent(){
    for(;;){
        WatchKey key;           
        try {
            key = this.watcher.take();
        } catch (InterruptedException ex){
            return;
        }

        for(WatchEvent<?> event: key.pollEvents()){
            WatchEvent<Path> newevent = (WatchEvent<Path>)event;
            Path filepath = newevent.context();

        ... clipped ....
        }

        boolean valid = key.reset();
        if(!valid){
            break;
        }
    }
Chiquia answered 14/10, 2013 at 2:27 Comment(1)
Thanks for feedback colson but I did include the reset(). Each class is identical but for the respective imports and specific differences.Guardroom
R
0

Though there's no really clear description about UNC Paths and remote file-systems in general, here is what I found out:

At first it seems to be possible to register a WatchKey, but immediately the WatchKey invalidates (WatchKey.isValid()).

Due to the straight connection to the FileSystem it's not possible to register a WatchKey to a remote location.

Possible workarounds:

1. Remote FileWatcher

Share a list of paths to watch to a remote jvm and let it forward the changes to your machine.

2. Polling

The other workaround would be a poller (only recomendable if not time-critical).
If you'd poll constantly, this would cause a lot of network-traffic.

Would be nice to either get an Exception when registering to a remote Path or at leaset read that in the When to Use and Not Use This API section.

Radiate answered 1/1, 2014 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.