Class DirectoryWatcher

java.lang.Object
org.forgerock.util.DirectoryWatcher

public class DirectoryWatcher extends Object
High-level interface to the WatchService API for detecting filesystem change events.
  • Constructor Details

    • DirectoryWatcher

      public DirectoryWatcher(ScheduledExecutorService scheduledExecutorService, WatchEvent.Modifier... modifiers) throws IOException
      Initializes the directory watcher. The watcher will use the given executor service to execute a periodic task polling for filesystem changes. The watch service from the default filesystem will be used to wait for events.
      Parameters:
      scheduledExecutorService - the scheduled executor service (never null). A periodic task will be added that polls for filesystem events every second.
      modifiers - how urgently events should be delivered. A higher value will result in more frequent polling of the filesystem in some implementations. The allowed values are JVM specific. On Hotspot use com.sun.nio.file.SensitivityWatchEventModifier. Leave unspecified to use the default sensitivity. Other modifiers may also be allowed depending on the platform.
      Throws:
      IOException - if an I/O error occurs when trying to construct the watch service.
  • Method Details

    • watch

      @SafeVarargs public final DirectoryWatcher.ChangeListener watch(Path directory, WatchEvent.Kind<Path>... eventKinds) throws IOException
      Registers a directory to be watched for change notifications.
      Parameters:
      directory - the directory to watch.
      eventKinds - the kinds of events to watch. See StandardWatchEventKinds for values.
      Returns:
      a change listener that can be used to add callbacks.
      Throws:
      IOException - if an I/O error prevents the directory being watched.
      IllegalStateException - if the watch service has already been shut down.
    • shutdown

      public void shutdown()
      Shuts down the directory watcher, cancelling all registered watch tasks and closing the underlying watch service.