Source:
public boolean processEvents() { if (watchKeyPathMap.isEmpty()) { return false; } WatchKey key; try { key = watchService.take(); } catch (ClosedWatchServiceException|InterruptedException ex) { LOG.info("Watch service interrupted or closed while waiting to get next watch key. Exiting!", ex); return false; } Path dir = watchKeyPathMap.get(key); if (dir == null) { LOG.info("Unrecognized watch key: " + key + ". Skipping the key without reseting it."); return true; } for (WatchEvent<?> event: key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { LOG.warn("Overflow event received for key: " + key + ". This means events have been missed or discarded. Please verify."); return true; } WatchEvent<Path> ev = (WatchEvent<Path>) event; Path name = ev.context(); Path child = dir.resolve(name); LOG.info(String.format("%s: %s%n", event.kind().name(), child)); try { if (kind == StandardWatchEventKinds.ENTRY_CREATE) { watchKeyFileEventHandlerMap.get(key).created(child); } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { watchKeyFileEventHandlerMap.get(key).modified(child); } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { watchKeyFileEventHandlerMap.get(key).deleted(child); } } catch (RuntimeException ex) { LOG.warn(String.format("Exception thrown by handler %s while processing event %s", watchKeyFileEventHandlerMap.get(key), event.kind().name()), ex); } } boolean valid = key.reset(); if (!valid) { LOG.info("Key " + key + " not being watched any more as it could not be reset."); watchKeyPathMap.remove(key); watchKeyFileEventHandlerMap.remove(key); } return true; }

Target:
public boolean processEvents() { if (watchKeyPathMap.isEmpty()) { return false; } WatchKey key; try { key = watchService.take(); } catch (ClosedWatchServiceException|InterruptedException ex) { LOG.info("Watch service interrupted or closed while waiting to get next watch key. Exiting!", ex); return false; } Path dir = watchKeyPathMap.get(key); if (dir == null) { LOG.info("Unrecognized watch key: " + key + ". Skipping the key without reseting it."); return true; } for (WatchEvent<?> event: key.pollEvents()) { WatchEvent.Kind kind = event.kind(); if (kind == StandardWatchEventKinds.OVERFLOW) { LOG.warn("Overflow event received for key: " + key + ". This means events have been missed or discarded. Please verify."); return true; } WatchEvent<Path> ev = (WatchEvent<Path>) event; Path name = ev.context(); Path child = dir.resolve(name); LOG.info("{}: {}", event.kind().name(), child); try { if (kind == StandardWatchEventKinds.ENTRY_CREATE) { watchKeyFileEventHandlerMap.get(key).created(child); } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { watchKeyFileEventHandlerMap.get(key).modified(child); } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { watchKeyFileEventHandlerMap.get(key).deleted(child); } } catch (RuntimeException ex) { LOG.warn("Exception thrown by handler while processing event {}", watchKeyFileEventHandlerMap.get(key), event.kind().name(), ex); } } boolean valid = key.reset(); if (!valid) { LOG.info("Key " + key + " not being watched any more as it could not be reset."); watchKeyPathMap.remove(key); watchKeyFileEventHandlerMap.remove(key); } return true; }