Class WatchManager

java.lang.Object
com.mastfrog.util.file.WatchManager

public final class WatchManager extends Object
Manages file system watches, centralizing management of watch keys, and enabling and disabling notifications; uses a single thread and a set of polling intervals, and timeouts to ensure all watches are fairly serviced with fixed overhead.
Author:
Tim Boudreau
  • Constructor Details

    • WatchManager

      public WatchManager(ScheduledExecutorService executor, Duration perKeyWait, Duration maxTimePer, Duration interval)
      Create a watch manager.
      Parameters:
      executor - The executor to poll and invoke callbacks in
      perKeyWait - The amount of time to wait for events on each key - if zero, polling will continue with no delay
      maxTimePer - The amount of time to spend on any given key before moving on to the next one, to avoid favoring one busy folder
      interval - The interval between polling runs (starts with the end of the previous polling run, not including the time that took)
    • WatchManager

      public WatchManager(Duration perKeyWait, Duration maxTimePer, Duration interval)
    • WatchManager

      public WatchManager()
    • WatchManager

      public WatchManager(ScheduledExecutorService executor, long perKeyWait, long maxTimePer, long interval)
  • Method Details

    • pause

      public boolean pause(boolean pause)
      Pause notification of file changes - no polling runs will occur; if a polling run is currently occurring, it will complete.
      Parameters:
      pause - If true, pause
      Returns:
      if the paused state changed
    • shutdown

      public boolean shutdown()
      Shut down this WatchManager, closing all watch keys.
      Returns:
      true if the state was changed
    • watch

      public void watch(Path target, BiConsumer<Path,WatchEvent.Kind<?>> c, WatchEvent.Kind<?> oneKind, WatchEvent.Kind<?>... more) throws IOException
      Watch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.

      Note that the consumer is weakly referenced in order that watch keys can be cleaned up if the listener is garbage collected.

      Parameters:
      target - The target file or folder
      c - The consumer
      oneKind - The first kind
      more - Additional kinds
      Throws:
      IOException - If something goes wrong
    • watch

      public void watch(Path target, BiConsumer<Path,WatchEvent.Kind<?>> c, Set<WatchEvent.Kind<?>> kinds) throws IOException
      Watch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.

      Note that the consumer is weakly referenced in order that watch keys can be cleaned up if the listener is garbage collected.

      Parameters:
      target - The target file or folder
      c - The consumer
      kinds - The kinds to listen to (must be non empty)
      Throws:
      IOException - If something goes wrong
    • unwatch

      public void unwatch(Path target, BiConsumer<Path,WatchEvent.Kind<?>> c)
      Stop watching a file or folder; if the passed consumer is null, this will unwatch for all listeners on the passed file.
      Parameters:
      target - The file or folder
      c - Optionally, a specific consumer to detach
    • isEmpty

      public boolean isEmpty()
      Determine if there are no active listeners on any files.
      Returns:
      true if there are none
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • recursiveWatch

      public WatchManager.RecursiveWatcher recursiveWatch(Path root, int maxDepth, BiConsumer<Path,WatchEvent.Kind<?>> consumer, WatchEvent.Kind<?> kind, WatchEvent.Kind<?>... more) throws IOException
      Create a new recursive watcher, already attached.
      Parameters:
      root - The root folder
      maxDepth - The depth to which to listen for file changes (keep as small as possible)
      consumer - The consumer
      kind - The first kind of event to watch for
      more - Other kinds of events to watch for
      Returns:
      A new watcher
      Throws:
      IOException