Package host.anzo.core.service
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a file system change event.static interfaceA listener interface for receiving file system change events. -
Method Summary
Modifier and TypeMethodDescriptionvoidInitiates an orderly shutdown of the file watcher service.voidregisterDirectory(Path directory, FileWatcherService.FileWatchListener listener, String... extensions) Registers a directory for monitoring with optional file extension filtering.voidregisterDirectory(Path directory, FileWatcherService.FileWatchListener listener, List<String> extensions) Registers a directory for monitoring with extension filtering.voidUnregisters all directories associated with the specified listener.
-
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 monitorlistener- The listener to receive file change eventsextensions- 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 monitorlistener- The listener to receive file change eventsextensions- List of file extensions to filter
-
unregisterListener
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:
onShutdownin interfacehost.anzo.commons.interfaces.startup.IShutdownable
-