We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
系统实现中经常需要能够感知配置文件的变化,然后及时更新上下文。
WatchService用来监控一个目录是否发生改变,但是可以通过 WatchEvent 上下文定位具体文件的变化。具体使用过程中要注意以下两点:
// 监控文件的变化,重新加载 executor.submit(new Runnable() { @Override public void run() { try { final Path path = FileSystems.getDefault().getPath(getMonitorDir()); System.out.println(path); final WatchService watchService = FileSystems.getDefault().newWatchService(); final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { final WatchKey wk = watchService.take(); for (WatchEvent<?> event : wk.pollEvents()) { final Path changed = (Path) event.context(); Path absolute = path.resolve(changed); File configFile = absolute.toFile(); long lastModified = configFile.lastModified(); logger.info(lastModified + "----------------"); // 利用文件时间戳,防止触发两次 if (changed.endsWith(getLicenseName()) && lastModified != LAST_MOD && configFile.length > 0) { logger.info("----------------- reloading -----------------"); LAST_MOD = lastModified; // 保存上一次时间戳 UPDATED = true; // 设置标志位 } } if (LICENSE_UPDATED) { reloadFile(); // 重新加载 } // reset the key boolean valid = wk.reset(); if (!valid) { logger.error("watch key invalid!"); } } } catch (Exception e) { logger.error(""); } } });
Watching a Directory for Changes
add 2017.9.5
The text was updated successfully, but these errors were encountered:
No branches or pull requests
场景
系统实现中经常需要能够感知配置文件的变化,然后及时更新上下文。
实现方案
使用 WatchService
WatchService用来监控一个目录是否发生改变,但是可以通过 WatchEvent 上下文定位具体文件的变化。具体使用过程中要注意以下两点:
参考
Watching a Directory for Changes
add 2017.9.5
The text was updated successfully, but these errors were encountered: