Class FileModificationEventWatcher

  • All Implemented Interfaces:
    FileModificationMonitor, java.lang.Runnable

    public class FileModificationEventWatcher
    extends java.lang.Thread
    implements FileModificationMonitor
    Watches for modifications to the specified file and performs the specified action (in the form of a callback) upon modification detection. This implementation uses NIO and event-based Java WatchService to watch for any modifications to the specified file. Note that: - The specified actions may trigger slightly later than when the file is actually modified. - If there are multiple file modifications in quick succession, only the last one may trigger the specified action. - Under rare circumstances, the callback is invoked twice upon file modification. The Java WatchService raises two events per modification: one for the content and second for the modification time. While there is a de-duplication logic in this class to ensure that only a single event is handled and notified to the callback, it may not work at times. If the calling code needs to be notified exactly once at all times, it should consider using FileModificationPollingMonitor instead. - This class won't work for a symbolic link file. This is because the Java WatchService does not raise events for modifications of such files. Consider using FileModificationPollingMonitor instead, when monitoring modifications to such files.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      FileModificationEventWatcher​(@NonNull java.nio.file.Path fileToWatch, @NonNull java.util.function.Consumer<java.nio.file.WatchEvent<?>> callback)
      Creates a new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void run()  
      void startMonitoring()
      Start monitoring.
      void stopMonitoring()
      Stop monitoring.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • FileModificationEventWatcher

        public FileModificationEventWatcher​(@NonNull
                                            @NonNull java.nio.file.Path fileToWatch,
                                            @NonNull
                                            @NonNull java.util.function.Consumer<java.nio.file.WatchEvent<?>> callback)
                                     throws java.io.FileNotFoundException
        Creates a new instance.
        Parameters:
        fileToWatch - the file to watch
        callback - the callback to invoke when a modification to the fileToWatch is detected
        Throws:
        java.lang.NullPointerException - if either fileToWatch or callback is null
        java.nio.file.InvalidPathException - if fileToWatch is invalid
        java.io.FileNotFoundException - when a file at specified path fileToWatch does not exist