Java 7 WatchService - Ignoring multiple occurrences of the same event

后端 未结 14 539
情歌与酒
情歌与酒 2020-12-05 02:21

The javadoc for StandardWatchEventKinds.ENTRY_MODIFY says:

Directory entry modified. When a directory is registered for this event the

相关标签:
14条回答
  • 2020-12-05 02:53

    If you are trying the same in Scala using better-files-akka library, I have comeup with this work around based on the solution proposed in the accepted answer.

    https://github.com/pathikrit/better-files/issues/313

    trait ConfWatcher {
    
      implicit def actorSystem: ActorSystem
    
      private val confPath = "/home/codingkapoor/application.conf"
      private val appConfFile = File(confPath)
      private var appConfLastModified = appConfFile.lastModifiedTime
    
      val watcher: ActorRef = appConfFile.newWatcher(recursive = false)
    
      watcher ! on(EventType.ENTRY_MODIFY) { file =>
        if (appConfLastModified.compareTo(file.lastModifiedTime) < 0) {
          // TODO
          appConfLastModified = file.lastModifiedTime
        }
      }
    
    }
    
    0 讨论(0)
  • 2020-12-05 02:56

    I had a similar issue - I am using the WatchService API to keep directories in sync, but observed that in many cases, updates were being performed twice. I seem to have resolved the issue by checking the timestamp on the files - this seems to screen out the second copy operation. (At least in windows 7 - I can't be sure if it will work correctly in other operation systems)

    Maybe you could use something similar? Store the timestamp from the file and reload only when the timestamp is updated?

    0 讨论(0)
提交回复
热议问题