Source:
static void setupWatch(final Map<String, ParseConfigFile> configFileMap) { Preconditions.checkArgument(configFileMap != null); Runnable runnable = () -> { WatchService watchService; Path path; for (Map.Entry<String, ParseConfigFile> entry : configFileMap.entrySet()) { String fileName = entry.getKey(); ParseConfigFile parser = entry.getValue(); Preconditions.checkArgument(fileName != null && parser != null); final File file = new File(fileName); if (!file.exists()) { throw new IllegalArgumentException("User provided file " + fileName + " does not exist."); } try { watchService = FileSystems.getDefault().newWatchService(); path = Paths.get(fileName).getParent(); path.register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH); } catch (IOException e) { log.warn("IOException while setting up watch on conf" + e.getMessage()); return; } for (; ; ) { WatchKey watchKey; try { watchKey = watchService.take(); } catch (InterruptedException ie) { log.warn(ie.getMessage()); return; } for (WatchEvent<?> event : watchKey.pollEvents()) { @SuppressWarnings("unchecked") final Path name = ((WatchEvent<Path>) event).context(); final Path child = path.resolve(name); if (!child.toString().equals(fileName)) { continue; } log.info("Modification detected, reloading user config"); parser.parseConfigFile(); } watchKey.reset(); } } }; final Thread thread = new Thread(runnable); log.info("Starting configuration watching thread."); thread.start(); }

Target:
static void setupWatch(final Map<String, ParseConfigFile> configFileMap) { Preconditions.checkNotNull(configFileMap); Preconditions.checkArgument(configFileMap.size() > 0); Runnable runnable = () -> { WatchService watchService = null; final Map<WatchKey, Path> keys = new HashMap<>(); final Multimap<Path, String> dirToFilesMap = HashMultimap.create(); for (Map.Entry<String, ParseConfigFile> entry : configFileMap.entrySet()) { String fileName = entry.getKey(); ParseConfigFile parser = entry.getValue(); Preconditions.checkNotNull(fileName); Preconditions.checkNotNull(parser); final File file = new File(fileName); if (!file.exists()) { log.warn("Failed to reload config, user provided file " + fileName + " does not exist."); continue; } try { watchService = FileSystems.getDefault().newWatchService(); Path dir = Paths.get(fileName).getParent(); if (!dirToFilesMap.containsKey(dir)) { WatchKey watchKey = dir.register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH); keys.put(watchKey, dir); } dirToFilesMap.put(dir, fileName); } catch (IOException e) { log.warn("IOException while setting up watch on conf " + fileName + ". " + e.getMessage()); return; } } if (watchService == null) { log.warn("Watchservice not setup for any config file(s)."); return; } for (;; ) { WatchKey watchKey; try { watchKey = watchService.take(); } catch (InterruptedException ie) { log.warn(ie.getMessage()); Thread.currentThread().interrupt(); return; } Path dir = keys.get(watchKey); for (WatchEvent<?> event : watchKey.pollEvents()) { @SuppressWarnings("unchecked") final Path name = ((WatchEvent<Path>) event).context(); final String filename = dir.resolve(name).toString(); if (dirToFilesMap.containsEntry(dir, filename)) { log.info("Modification detected, reloading config file " + filename); configFileMap.get(filename).parseConfigFile(); break; } } watchKey.reset(); } }; final Thread thread = new Thread(runnable); log.info("Starting configuration watching thread."); thread.start(); }