Package org.confng.reload
Class FileWatchService
- java.lang.Object
-
- org.confng.reload.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 Summary
Constructors Constructor Description FileWatchService(Consumer<Path> onFileChange)Creates a new file watch service with default 1 second debounce.FileWatchService(Consumer<Path> onFileChange, long debounceMs)Creates a new file watch service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Set<Path>getWatchedFiles()Gets the set of files being watched.booleanisRunning()Checks if the service is currently running.voidstart()Starts the file watching service.voidstop()Stops the file watching service.voidwatchFile(Path filePath)Registers a file to be watched for changes.
-
-
-
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 changesdebounceMs- 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.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
isRunning
public boolean isRunning()
Checks if the service is currently running.
-
-