Class FileWatcherService

java.lang.Object
host.anzo.core.service.FileWatcherService
All Implemented Interfaces:
host.anzo.commons.interfaces.startup.IShutdownable

public class FileWatcherService extends Object implements host.anzo.commons.interfaces.startup.IShutdownable
A singleton service for monitoring file system changes in registered directories. Supports recursive directory monitoring with file extension filtering and asynchronous event notifications.

Example usage:


 // Register a listener for JSON files in a directory
 FileWatchListener listener = new FileWatchListener() {
      void onFileChanged(FileWatchEvent event) {
          void onFileChanged(FileWatchEvent event) {
          }
      }
 }

 FileWatcherService.getInstance().registerDirectory(
     Paths.get("/path/to/dir"),
     listener,
     ".json"
 );

 // Unregister listener
 FileWatcherService.getInstance().unregisterListener(listener);

 // Shutdown the service when no longer needed
 FileWatcherService.getInstance().shutdown();
 
 
Since:
05.04.2025
  • Method Details

    • registerDirectory

      public void registerDirectory(Path directory, FileWatcherService.FileWatchListener listener, String... extensions)
      Registers a directory for monitoring with optional file extension filtering. Recursively registers all subdirectories found at the time of registration.
      Parameters:
      directory - The directory path to monitor
      listener - The listener to receive file change events
      extensions - Varargs of file extensions to filter (e.g., "txt", "log")
    • registerDirectory

      public void registerDirectory(Path directory, FileWatcherService.FileWatchListener listener, List<String> extensions)
      Registers a directory for monitoring with extension filtering. This overload accepts a list of extensions instead of varargs.
      Parameters:
      directory - The directory path to monitor
      listener - The listener to receive file change events
      extensions - List of file extensions to filter
    • unregisterListener

      public void unregisterListener(FileWatcherService.FileWatchListener listener)
      Unregisters all directories associated with the specified listener. Stops receiving events for all directories monitored by this listener.
      Parameters:
      listener - The listener to unregister
    • onShutdown

      public void onShutdown()
      Initiates an orderly shutdown of the file watcher service.
      Specified by:
      onShutdown in interface host.anzo.commons.interfaces.startup.IShutdownable