diff options
| author | Szymon Szukalski <szymon.szukalski@gmail.com> | 2012-10-03 10:45:23 +1000 |
|---|---|---|
| committer | Szymon Szukalski <szymon.szukalski@gmail.com> | 2012-10-03 10:45:23 +1000 |
| commit | 6ddaaf8555d6b94684d061289249cac67c6eb09f (patch) | |
| tree | ab4041586a70dcd1140cb32263c71354c2901e2b /src/main/java/io/skas/melbjvm/nio2/Watcher.java | |
| parent | 9c632725afb73bbf0966e52ca50c6d66c12d853b (diff) | |
Added example of SimpleFileVisitor usage
Diffstat (limited to 'src/main/java/io/skas/melbjvm/nio2/Watcher.java')
| -rw-r--r-- | src/main/java/io/skas/melbjvm/nio2/Watcher.java | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/main/java/io/skas/melbjvm/nio2/Watcher.java b/src/main/java/io/skas/melbjvm/nio2/Watcher.java index 1938c9f..c3986a7 100644 --- a/src/main/java/io/skas/melbjvm/nio2/Watcher.java +++ b/src/main/java/io/skas/melbjvm/nio2/Watcher.java @@ -13,11 +13,12 @@ public class Watcher { private static final Logger LOG = LoggerFactory.getLogger(Watcher.class); - public void watchDirectory(Path watchedPath) throws IOException, InterruptedException { + public void watchDirectory(Path directory) throws IOException, InterruptedException { + // New try with resources try (WatchService watchService = FileSystems.getDefault().newWatchService()) { - final WatchKey key = watchedPath.register( + final WatchKey key = directory.register( watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, @@ -25,21 +26,29 @@ public class Watcher { while (true) { + // retrieve and remove the next watch key (waits) watchService.take(); + // get list of events for the key for (WatchEvent<?> watchEvent : key.pollEvents()) { + + // get the event kind final WatchEvent.Kind<?> kind = watchEvent.kind(); + // get the filename for the event + final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; + final Path filename = watchEventPath.context(); + + LOG.debug("event: {} filename: {}", kind, filename); + // handle OVERFLOW event if (kind == StandardWatchEventKinds.OVERFLOW) { continue; } - final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent; - Path newFile = watchedPath.resolve(watchEventPath.context()); - + // handle CREATE event if (kind == StandardWatchEventKinds.ENTRY_CREATE) { - new AsynchronousFileReader(newFile); + new AsynchronousFileReader(filename); } } |
