Class FileWatcher

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class FileWatcher
    extends java.lang.Object
    implements java.lang.AutoCloseable
    File watcher helper for use with simple callbacks. It monitors two types of changes: Actual file changes, and symlink changes. Note that whenever a file or symlink is added to the whole, it will be monitored at least until the file or symlink is deleted. There are essentially two types of watchers. File specific watchers will always resolve back to the "requested" file, and it is triggered whenever any of:
    • The current canonical file content changed.
    • The link starts pointing to a different file, this is only calculated based on the file being a symlink, or being in a symlinked directory. Everything else is treated as canonical locations.

    So if this is the case (ref configMaps in kubernetes):

    
     /volume/map/..2018-01/config.txt (old file)
     /volume/map/..2018-02/config.txt (new file)
     /volume/map/..data     -> symlink to '..2018-01'
     /volume/map/config.txt -> symlink to '..data/config.txt'
     
    If you listen to '/volume/map/config.txt', then you are notified if:
    • '/volume/map/..2018-01/config.txt' is changed
    • '/volume/map/..data' symlink is updated to '..2018-02'
    • '/volume/map/config.txt' symlink is updated to '..2018-02/config.txt'
    The important case here is the middle one, as this is the way kubernetes handles its configMaps and Secrets.
    • Constructor Detail

      • FileWatcher

        public FileWatcher()
        Create a FileWatcher with default watch service.
      • FileWatcher

        public FileWatcher​(java.nio.file.WatchService watchService)
        Create a FileWatcher using the provided watch service.
        Parameters:
        watchService - Watcher service to use.
      • FileWatcher

        protected FileWatcher​(java.nio.file.WatchService watchService,
                              java.util.concurrent.ExecutorService watcherExecutor,
                              java.util.concurrent.ExecutorService callbackExecutor)
    • Method Detail

      • addWatcher

        @Deprecated
        public void addWatcher​(java.io.File file,
                               FileWatcher.Watcher watcher)
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • addWatcher

        @Deprecated
        public void addWatcher​(java.io.File file,
                               FileWatcher.Listener watcher)
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • addWatcher

        @Deprecated
        public void addWatcher​(java.nio.file.Path file,
                               FileWatcher.Watcher watcher)
        Deprecated.
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • addWatcher

        public void addWatcher​(java.nio.file.Path file,
                               FileWatcher.Listener watcher)
        Start watching file path and notify watcher for updates on that file.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • addWatcher

        @Deprecated
        public void addWatcher​(FileWatcher.Watcher watcher)
        Add a file watcher that is persistent. If the reference from the file watcher to the watcher itself should not prevent garbage collection, use the weakAddWatcher(Listener) method.
        Parameters:
        watcher - The watcher to add.
      • weakAddWatcher

        @Deprecated
        public void weakAddWatcher​(java.io.File file,
                                   FileWatcher.Watcher watcher)
        Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • weakAddWatcher

        @Deprecated
        public void weakAddWatcher​(java.io.File file,
                                   FileWatcher.Listener watcher)
        Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • weakAddWatcher

        @Deprecated
        public void weakAddWatcher​(java.nio.file.Path file,
                                   FileWatcher.Watcher watcher)
        Deprecated.
        Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • weakAddWatcher

        public void weakAddWatcher​(java.nio.file.Path file,
                                   FileWatcher.Listener watcher)
        Start watching file path and notify watcher for updates on that file. The watcher will be kept in a weak reference and will allow GC to delete the instance.
        Parameters:
        file - The file path to watch.
        watcher - The watcher to be notified.
      • removeWatcher

        @Deprecated
        public boolean removeWatcher​(FileWatcher.Watcher watcher)
        Deprecated.
        Remove a watcher from the list of listeners.
        Parameters:
        watcher - The watcher to be removed.
        Returns:
        True if the watcher was removed from the list.
      • removeWatcher

        public boolean removeWatcher​(FileWatcher.Listener watcher)
        Remove a watcher from the list of listeners.
        Parameters:
        watcher - The watcher to be removed.
        Returns:
        True if the watcher was removed from the list.
      • startWatching

        @Deprecated
        public void startWatching​(java.io.File file)
        Start watching a specific file. Note that this will watch the file as seen in the directory as it is pointed to. This means that if the file itself is a symlink, then change events will notify changes to the symlink definition, not the content of the file.
        Parameters:
        file - The file to be watched.
      • stopWatching

        @Deprecated
        public void stopWatching​(java.io.File file)
        Deprecated.
        Use removeWatcher(Listener) and let it clean up watched files itself.
        Stop watching a specific file.
        Parameters:
        file - The file to be watched.
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable