I have Java program monitoring a remote folder mounted in my local server. But it is not detecting any changes / modification whenever something changed in the remote folder.
It is working fine if the changes / modification is made in the mounted folder.
Searching through net, as mention in the Java docs
If a watched file is not located on a local storage device then it is implementation specific if changes to the file can be detected. In particular, it is not required that changes to files carried out on remote systems be detected.
Anyone could help provide me sample on how to do this? below is my current code
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(directory);
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (Exception ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
if (kind == ENTRY_MODIFY) {
System.out.println("file has changed");
// other process
}
if (kind == ENTRY_CREATE) {
System.out.println("file has created");
// other process
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}