Class WatchDir

java.lang.Object
com.helger.commons.io.watchdir.WatchDir
All Implemented Interfaces:
AutoCloseable

public class WatchDir extends Object implements AutoCloseable
Generic directory watching service using the default JDK WatchService class.
Since:
9.0.0
Author:
Philip Helger
  • Constructor Details

    • WatchDir

      public WatchDir(@Nonnull Path aDir, boolean bRecursive) throws IOException
      Creates a WatchService and registers the given directory
      Parameters:
      aDir - The directory to be watched. May not be null.
      bRecursive - true to watch the directory recursive, false to watch just this directory.
      Throws:
      IOException - In case something goes wrong.
  • Method Details

    • callbacks

      Returns:
      The modifiable callback list. Never null.
    • getStartDirectory

      @Nonnull public Path getStartDirectory()
      Returns:
      The start directory as specified in the constructor. Never null.
    • isRecursive

      public boolean isRecursive()
      Returns:
      true if this is a recursive listener, false if not.
    • close

      public void close() throws IOException
      Close the watch directory service and stop processing.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • stopProcessing

      public void stopProcessing()
      Stop processing, if processEvents() is active. This method is automatically called in close().
    • isProcessing

      public boolean isProcessing()
      Check if processing is active.
      Returns:
      true if event processing is active, false if not.
      See Also:
    • processEvents

      public void processEvents()
      Process all events for keys queued to the watcher. Call stopProcessing() or close() to stop processing within a reasonable time. This method should run in a separate thread, as it contains an infinite loop! Usually you don't call this method manually.
      See Also:
    • runAsync

      public void runAsync()
      Call this method to process events. This method creates a background thread than runs processEvents() and performs the heavy lifting.
    • runAsyncAndReturn

      @Nonnull public Thread runAsyncAndReturn()
      Call this method to process events. This method creates a background thread than runs processEvents() and performs the heavy lifting.
      Returns:
      The created Thread that can also be stopped again if not needed anymore.
      Since:
      10.1.5
    • createAsyncRunningWatchDir

      @Nonnull @ReturnsMutableCopy public static WatchDir createAsyncRunningWatchDir(@Nonnull Path aDir, boolean bRecursive, @Nonnull IWatchDirCallback aCallback) throws IOException
      Static factory method to create a simple WatchDir instance that already spawned an Thread to listen. To close the thread call the close() method.
      Parameters:
      aDir - The directory to be watched. May not be null.
      bRecursive - true to watch the directory recursive, false to watch just this directory.
      aCallback - The callback to be invoked if something changed. May not be null.
      Returns:
      The newly created WatchDir instance and never null.
      Throws:
      IOException - In case something goes wrong.