Java NIO Viewer created "ENTRY_CREATE" and "ENTRY_MODIFY" when a new file is added to the viewer

I see strange behavior (not sure if this is the expected behavior) using java.nio.file.WatchService.

The problem is that I have a folder registered in WatchService. When I copy a new file to this folder, two WatchEvents are generated, one for:

'ENTRY_CREATE' and "ENTRY_MODIFY".

As far as I understand, a new file (copied from another directory that cannot be viewed) should create only one event: ie: ENTRY_CREATE.

Can someone explain why the extra event "ENTRY_MODIFY" is being created?

My code is:

public void watch() { WatchKey key = watcher.poll(); //log.info("Watcher scheduler running. Watch key {}", key.hashCode()); if (key != null) { Workflow workflow = keys.get(key); log.info("Runing watcher for key '{}' and workflow {}", key.hashCode(), workflow.getName()); File hotFolder = new File(workflow.getFolderPath()); Path dir = hotFolder.toPath(); for (WatchEvent<?> event : key.pollEvents()) { WatchEvent<Path> ev = cast(event); Path name = ev.context(); Path child = dir.resolve(name); log.info("Polling event for name {} and child {} and dir {}", name.toFile(), child.toFile(), dir.toFile()); if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) continue; try { switch (event.kind().name()) { case "ENTRY_CREATE": log.info("New file {}", child.toFile()); fileService.processNewFile(child.toFile(), workflow); break; case "ENTRY_MODIFY": log.info("File modified.... {}", child.toFile()); fileService.processModifiedFile(child.toFile(), workflow); break; default: log.error("Unknown event {} for file {}", event.kind() .name(), child.toFile()); break; } // Operation op = Operation.from(event.kind()); // if (op != null) // publisher.publishEvent(new FileEvent(child.toFile(), // workflow, op)); } catch (Throwable t) { log.warn("Error while procesing file event", t); } } key.reset(); } } 

So when I copy the file, say name = "abc.txt", the log is displayed:
New abc.txt file
File modified .... abc.txt


Any inputs are very requested.

+5
source share
2 answers

Check out the "Platform Dependencies" section in WatchService JavaDoc .

The observer behaves correctly in the described situation. And, strictly speaking, when you copy a file to a folder, the files are really CREATED and then MODIFIED.

When using WatcherService, there are many things whose behavior varies greatly, for example:

  • OS
  • Encrypted drives
  • Network resources
  • ...
+4
source

I think this question has already been answered.

For example Java: WatchService gets information before copying content

In a nutshell, events are generated by your operating system. You have two events that mean that your OS is copying in a non-atomic way. If you try to read the file somewhere between ENTRY_CREATE and ENTRY_MODIFY, it will be inconsistent. But then again, it depends on your OS. For example, on Windows 7, I get ENTRY_CREATE once and ENTRY_MODIFY twice for large files. Thus, you really cannot be sure if the file was copied successfully, you need to come up with application / OS specific metrics.

+3
source

Source: https://habr.com/ru/post/1236128/


All Articles