Class FileWatchService

  • All Implemented Interfaces:
    AutoCloseable

    public class FileWatchService
    extends Object
    implements AutoCloseable
    Background service that watches configuration files for changes.

    This service uses Java's WatchService API to monitor directories containing configuration files. When a file changes, it triggers a reload of the configuration.

    Since:
    1.1.0
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    • Constructor Detail

      • FileWatchService

        public FileWatchService​(Consumer<Path> onFileChange,
                                long debounceMs)
                         throws IOException
        Creates a new file watch service.
        Parameters:
        onFileChange - callback invoked when a watched file changes
        debounceMs - minimum time between reload triggers for the same file
        Throws:
        IOException - if the watch service cannot be created
      • FileWatchService

        public FileWatchService​(Consumer<Path> onFileChange)
                         throws IOException
        Creates a new file watch service with default 1 second debounce.
        Throws:
        IOException
    • Method Detail

      • watchFile

        public void watchFile​(Path filePath)
                       throws IOException
        Registers a file to be watched for changes.
        Parameters:
        filePath - the file to watch
        Throws:
        IOException - if the file's directory cannot be watched
      • start

        public void start()
        Starts the file watching service.
      • stop

        public void stop()
        Stops the file watching service.
      • isRunning

        public boolean isRunning()
        Checks if the service is currently running.
      • getWatchedFiles

        public Set<Path> getWatchedFiles()
        Gets the set of files being watched.