Class PathWatcher

All Implemented Interfaces:
Runnable, LifeCycle

public class PathWatcher extends AbstractLifeCycle implements Runnable
Watch a Path (and sub directories) for Path changes.

Suitable replacement for the old Scanner implementation.

Allows for configured Excludes and Includes using FileSystem.getPathMatcher(String) syntax.

Reports activity via registered PathWatcher.Listeners

  • Field Details

  • Constructor Details

    • PathWatcher

      public PathWatcher()
      Construct new PathWatcher
  • Method Details

    • cast

      protected static <T> WatchEvent<T> cast(WatchEvent<?> event)
    • getConfigs

      public Collection<PathWatcher.Config> getConfigs()
    • watch

      public void watch(Path file)
      Request watch on a the given path (either file or dir) using all Config defaults. In the case of a dir, the default is not to recurse into subdirs for watching.
      Parameters:
      file - the path to watch
    • watch

      public void watch(PathWatcher.Config config)
      Request watch on a path with custom Config provided.
      Parameters:
      config - the configuration to watch
    • addListener

      public void addListener(EventListener listener)
      Add a listener for changes the watcher notices.
      Parameters:
      listener - change listener
    • appendConfigId

      private void appendConfigId(StringBuilder s)
      Append some info on the paths that we are watching.
    • doStart

      protected void doStart() throws Exception
      Overrides:
      doStart in class AbstractLifeCycle
      Throws:
      Exception
      See Also:
    • doStop

      protected void doStop() throws Exception
      Overrides:
      doStop in class AbstractLifeCycle
      Throws:
      Exception
      See Also:
    • reset

      public void reset()
      Remove all current configs and listeners.
    • isNotifiable

      protected boolean isNotifiable()
      Check to see if the watcher is in a state where it should generate watch events to the listeners. Used to determine if watcher should generate events for existing files and dirs on startup.
      Returns:
      true if the watcher should generate events to the listeners.
    • getListeners

      public Iterator<EventListener> getListeners()
      Get an iterator over the listeners.
      Returns:
      iterator over the listeners.
    • getUpdateQuietTimeMillis

      public long getUpdateQuietTimeMillis()
      Change the quiet time.
      Returns:
      the quiet time in millis
    • registerTree

      private void registerTree(Path dir, PathWatcher.Config config, boolean notify) throws IOException
      Throws:
      IOException
    • registerDir

      private void registerDir(Path path, PathWatcher.Config config) throws IOException
      Throws:
      IOException
    • register

      protected void register(Path path, PathWatcher.Config config) throws IOException
      Throws:
      IOException
    • register

      private void register(Path path, PathWatcher.Config config, WatchEvent.Kind<?>[] kinds) throws IOException
      Throws:
      IOException
    • removeListener

      public boolean removeListener(PathWatcher.Listener listener)
      Delete a listener
      Parameters:
      listener - the listener to remove
      Returns:
      true if the listener existed and was removed
    • run

      public void run()
      Forever loop. Wait for the WatchService to report some filesystem events for the watched paths. When an event for a path first occurs, it is subjected to a quiet time. Subsequent events that arrive for the same path during this quiet time are accumulated and the timer reset. Only when the quiet time has expired are the accumulated events sent. MODIFY events are handled slightly differently - multiple MODIFY events arriving within a quiet time are coalesced into a single MODIFY event. Both the accumulation of events and coalescing of MODIFY events reduce the number and frequency of event reporting for "noisy" files (ie those that are undergoing rapid change).
      Specified by:
      run in interface Runnable
      See Also:
    • handleKey

      private void handleKey(WatchKey key)
    • handleWatchEvent

      public void handleWatchEvent(Path path, PathWatcher.PathWatchEvent event)
      Add an event reported by the WatchService to list of pending events that will be sent after their quiet time has expired.
      Parameters:
      path - the path to add to the pending list
      event - the pending event
    • processPending

      private long processPending()
    • notifyEvents

      private void notifyEvents()
    • setNotifyExistingOnStart

      public void setNotifyExistingOnStart(boolean notify)
      Whether or not to issue notifications for directories and files that already exist when the watcher starts.
      Parameters:
      notify - true if existing paths should be notified or not
    • isNotifyExistingOnStart

      public boolean isNotifyExistingOnStart()
    • setUpdateQuietTime

      public void setUpdateQuietTime(long duration, TimeUnit unit)
      Set the quiet time.
      Parameters:
      duration - the quiet time duration
      unit - the quite time unit
    • toString

      public String toString()
      Overrides:
      toString in class AbstractLifeCycle