Java 7 NIO watchservice vs jpathwatch

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

\\otherMach\path\to\watch. 

To test, I injected the code from the Java NIO training site http://docs.oracle.com/javase/tutorial/essential/io/fileio.html , and then duplicated the class exchange in the jpathwwatch import instead of the Java NIO import, jpathwatch works for UNC paths, but Java NIO does not. It seems to register and even return the 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 does not recognize further changes.

jpathwatch logs and reports the directory and events of the file (although it does not report the start event immediately after registration).

 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 the fact that you see in the jpathwatch discussion that network monitoring is NOT supported - pay attention to Uwe Pachler, referring to UNC paths - http://sourceforge.net/p/jpathwatch/discussion/888207/thread/8ea778de/? limit = 25 # 0037

Is anyone lucky with viewing UNC paths and Java 7 NIO.2? Any other or later solutions?

Thanks,

-mjash

+8
java nio nio2 watchservice
source share
3 answers

It looks like this is a bug in the JDK that has been fixed in JDK 1.7.0_u60. I just tried using u71 (used u45) and now it checks to see if it works for me through UNC.

+2
source share

If you use the tutorial and examples from the Oracle documentation on WatchEvent, you may have missed the key.reset () call after the event was passed. I just ran into the same problem:

  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; } } 
0
source share

Although there is no clear description of UNC paths and remote file systems in general, here is what I learned:

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

Due to the direct connection to the FileSystem, it is not possible to register WatchKey at a remote location.

Possible workarounds:

1. Remote FileWatcher

Share a list of paths to view the remote jvm and let it redirect the changes to your computer.

2. Poll

Another workaround would be poller (only recommended, if not time critical).
If you interrogate constantly, it will cause a lot of network traffic.

It would be nice to either get an exception when registering for the remote Path, or in a sheet, read this in the When to Use and Not Use This API section.

0
source share

All Articles