Tue Feb 14, 2012 9:59 pm by lampsound
A little test:
- Code:
import java.nio.file.*;
import java.util.List;
public class WatcherTest {
private static final String PATH = "\\\\POPCORN\\ShareCenter\\Video";
// private static final String PATH = "\\\\STORAGE\\ShareCenter\\Public";
public static void main(String[] args) throws Exception {
FileSystem fs = FileSystems.getDefault();
WatchService ws = fs.newWatchService();
System.out.println(String.format("Start polling for [%s]", PATH));
Path path = fs.getPath(PATH);
path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW, StandardWatchEventKinds.ENTRY_DELETE);
WatchKey watchKey;
do {
watchKey = ws.take();
List<WatchEvent<?>> events = watchKey.pollEvents();
for (WatchEvent object : events) {
if (object.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
System.out.println("Modify: " + object.context().toString());
}
if (object.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)) {
System.out.println("Delete: " + object.context().toString());
}
if (object.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) {
System.out.println("Created: " + object.context().toString());
}
}
} while (watchKey.reset());
}
}
The results for application running with JDK7-final on Windows 7 x64 are:
1. WatchService correctly handles changes on remote drive hosted at Windows-based PC (\\STORAGE in example)
2. WatchService exits immediately (ws.take() returns empty list and watchKey.reset() returns 'false') on remote drive hosted at Linux-based device with Samba (\\POPCORN in example)