Class AFileWatcher

java.lang.Object
de.xima.fc.config.AFileWatcher

public abstract class AFileWatcher extends Object
A watcher that runs an action each time at least one of the watched files have changed. This implementation uses a ScheduledExecutorService to poll the files for changes in fixed intervals.

You may either use one of the static methods of this class to watch a file; or extend this class and override the runOnChange(Iterable) method.

Please note that the action is not run initially, only when changes occur from now on.

Author:
XIMA MEDIA GmbH
  • Constructor Details

    • AFileWatcher

      protected AFileWatcher(long delay, TimeUnit unit, String name, Iterable<File> files)
      Creates a new file watcher that periodically checks the given files for changes; and invokes runOnChange(Iterable) once at least one file has changed.
      Parameters:
      delay - Delay between checks.
      unit - Time unit of the delay.
      name - A name for this file watcher, also used for naming the threads.
      files - File to check for changes.
  • Method Details

    • shutdownAndAwaitTermination

      public boolean shutdownAndAwaitTermination(int timeout, TimeUnit unit) throws InterruptedException
      Blocks until this watcher terminates, or the timeout is reached.
      Parameters:
      timeout - Maximum time to wait for termination.
      unit - Time unit for the timeout.
      Returns:
      true if this watcher is now terminated, false otherwise.
      Throws:
      InterruptedException - if interrupted while waiting
      See Also:
    • shutdown

      public void shutdown()
      Requests this watcher to terminate, and returns immediately.
      See Also:
    • runOnChange

      protected abstract void runOnChange(Iterable<File> files)
      An action that is called when one or more of the watched files have changed.
      Parameters:
      files - A list of files that have changed.
    • watchSingle

      public static AFileWatcher watchSingle(long delay, TimeUnit unit, File file, String name, Runnable action) throws Exception
      Watches a single file for changes via polling for changes in fixed intervals. The given action is run each time the file has changed.

      Please note that the action is not run initially, only when changes occur from now on.

      Parameters:
      delay - Duration between pollings.
      unit - Time unit for the given duration.
      file - A file to watch for changes.
      name - A name for the action, also used for naming the threads.
      action - An action to run each time the file has changed.
      Returns:
      A watcher instance for stopping the watch.
      Throws:
      Exception - When the watcher could not be started.