Class SingleFileChangeWatcher

java.lang.Object
de.s42.base.files.SingleFileChangeWatcher

public class SingleFileChangeWatcher extends Object
Allows to watch changes on a given single normal file. The easiest way to use it is:
 
 Pair<SingleFileChangeWatcher, Thread> watcher = SingleFileChangeWatcher.startWatching(file, (file) -> {
   // ... do something with changed file ...
 });
 
 
Author:
Benjamin Schiller
  • Field Details

    • file

      protected final Path file
      File to watch changes on.
    • handler

      protected final Consumer<Path> handler
      handler that will be called back on changes of file.
    • watcher

      protected WatchService watcher
      FS integration for watching changes
    • terminated

      protected boolean terminated
      will be set to true on cancel().
  • Constructor Details

    • SingleFileChangeWatcher

      public SingleFileChangeWatcher(Path file, Consumer<Path> handler)
      Create a watcher for the given file and given a callback that will be called whenever the file has changed in the filesystem
      Parameters:
      file -
      handler -
  • Method Details

    • startWatching

      public static Pair<SingleFileChangeWatcher, Thread> startWatching(Path file, Consumer<Path> handler)
      Creates and starts the watcher. The thread is a virtual thread.
      Parameters:
      file -
      handler -
      Returns:
    • cancel

      public void cancel() throws IOException
      Allows to cancel the running watcher
      Throws:
      IOException
    • startWatching

      public Thread startWatching() throws RuntimeException
      Starts this watcher creating a virtual thread.
      Returns:
      Throws:
      RuntimeException
    • watchImmediate

      public void watchImmediate() throws Exception
      Can be called by a worker directly. ATTENTION this method will block until the watcher is cancellde or invalidated! It is intended to be used if you want to control the execution (own runners, thread, etc.).
      Throws:
      Exception