Class FileWatcher


  • public class FileWatcher
    extends AbstractLifecycle
    Creates a WatchService from the injected FileSystem and registers it to the injected Path to watch. When started, instances of this class will schedule polling tasks on the injected SchedulerAbstraction to check whether or not the directory represented by the injected path to watch exists and is a valid directory and if so, will start watching that directory and will call the injected listener with a FileWatcher.FileWatchEventType.REGISTER event type and a null path, else it will continue polling until the directory is created or it is shut down. Once the directory is being watched, if a file was created, modified, or deleted in the watched directory in between each poll cycle, then the respective FileWatcher.FileWatchEventType will be passed to the injected listener along with the full path to the file for each watch event which occurred for that poll cycle. If there is an overflow of events from the FileSystem, the injected listener will be called with a FileWatcher.FileWatchEventType.OVERFLOW event type and a null path. This watcher handles the case where the path to watch is deleted while it is being watched. In this case, it falls back to the above mentioned behavior when the watcher is started up and the path to watch does not represent a directory or does not exist. If the path to watch is re-created as a valid directory, the watcher will resume watching the path for changes and will notify the listener with a FileWatcher.FileWatchEventType.REGISTER event type and a null path.
    • Constructor Detail

      • FileWatcher

        public FileWatcher​(@Nonnull
                           java.nio.file.FileSystem fileSystem,
                           @Nonnull
                           java.nio.file.Path pathToWatch,
                           @Nonnull
                           SchedulerAbstraction scheduler,
                           @Nonnull
                           java.util.function.BiConsumer<FileWatcher.FileWatchEventType,​java.nio.file.Path> listener)
        The primary constructor for creating instances of the FileWatcher.
        Parameters:
        fileSystem - The FileSystem which has the injected path which will be watched
        pathToWatch - The Path to watch
        scheduler - The SchedulerAbstraction used to schedule polling tasks
        listener - The BiConsumer listener which will be called when the watcher is registered with the path to watch and for any watch events which occur in the watched path per poll cycle