源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java使用WatchService监控文件内容变化的示例

  • 时间:2020-04-20 19:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java使用WatchService监控文件内容变化的示例
[b]场景[/b] 系统实现中经常需要能够感知配置文件的变化,然后及时更新上下文。 [b]实现方案[/b] [list] [*]自己起一个单独线程,定时加载文件,实现较简单,但是无法保证能够实时捕捉文件变化,同时耗CPU[/*] [*]使用commons-io中的 FileAlterationObserver,思想和上面类似,对比前后文件列表的变化,触发对应事件[/*] [*]JDK 1.7提供的WatchService,利用底层文件系统提供的功能[/*] [/list] [b]使用 WatchService[/b] WatchService用来监控一个目录是否发生改变,但是可以通过 WatchEvent 上下文定位具体文件的变化。具体使用过程中要注意以下两点: [list] [*]文件改变可能会触发两次事件(我的理解:文件内容的变更,元数据的变更),可以通过文件的时间戳来控制[/*] [*]在文件变化事件发生后,如果立即读取文件,可能所获内容并不完整,建议的做法判断文件的 length > 0 [/*] [/list]
// 监控文件的变化,重新加载
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 (UPDATED) {
          reloadFile(); // 重新加载
        }
        // reset the key
        boolean valid = wk.reset();
        if (!valid) {
          logger.error("watch key invalid!");
        }
      }
    } catch (Exception e) {
      logger.error("");
    }
  }
});
参考 [url=https://docs.oracle.com/javase/tutorial/essential/io/notification.html#concerns]Watching a Directory for Changes[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部