Package io.microsphere.io
Class StandardFileWatchService
- java.lang.Object
-
- io.microsphere.io.StandardFileWatchService
-
- All Implemented Interfaces:
FileWatchService
,java.lang.AutoCloseable
public class StandardFileWatchService extends java.lang.Object implements FileWatchService, java.lang.AutoCloseable
/** Standard implementation of theFileWatchService
interface using JDK 7'sWatchService
API. This class monitors files or directories for creation, modification, and deletion events.Key Features
- Supports watching individual files or entire directories.
- Allows filtering based on event types: CREATED, MODIFIED, DELETED.
- Uses a thread-safe design to handle concurrent listeners and events.
- Provides auto-closeable behavior via the
close()
method.
Example Usage
// Example 1: Watching a single file for modification events FileWatchService watchService = new StandardFileWatchService(); File fileToWatch = new File("/path/to/file.txt"); FileChangedListener listener = event -> System.out.println("File changed: " + event.getFile()); watchService.watch(fileToWatch, listener, FileChangedEvent.Kind.MODIFIED); // Example 2: Watching a directory for all types of events with multiple listeners File dirToWatch = new File("/path/to/directory"); List<FileChangedListener> listeners = Arrays.asList( event -> System.out.println("Listener 1 triggered: " + event), event -> System.out.println("Listener 2 triggered: " + event) ); watchService.watch(dirToWatch, listeners); // All kinds by default
Make sure to call
close()
when you're done using the service to release resources.- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
WatchService
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_THREAD_NAME_PREFIX
The default thread name prefix : "microsphere-file-watch-service"static java.lang.String
THREAD_NAME_PREFIX
The thread name prefix , default value : "microsphere-file-watch-service-"static java.lang.String
THREAD_NAME_PREFIX_PROPERTY_NAME
The thread name prefix property name : "microsphere.file-watch-service.thread-name-prefix
-
Constructor Summary
Constructors Constructor Description StandardFileWatchService()
StandardFileWatchService(java.util.concurrent.Executor eventHandlerExecutor)
StandardFileWatchService(java.util.concurrent.Executor eventHandlerExecutor, java.util.concurrent.ExecutorService eventLoopExecutor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
start()
void
stop()
void
watch(java.io.File file, FileChangedListener listener, FileChangedEvent.Kind... kinds)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.microsphere.io.FileWatchService
watch
-
-
-
-
Field Detail
-
DEFAULT_THREAD_NAME_PREFIX
public static final java.lang.String DEFAULT_THREAD_NAME_PREFIX
The default thread name prefix : "microsphere-file-watch-service"- See Also:
- Constant Field Values
-
THREAD_NAME_PREFIX_PROPERTY_NAME
public static final java.lang.String THREAD_NAME_PREFIX_PROPERTY_NAME
The thread name prefix property name : "microsphere.file-watch-service.thread-name-prefix- See Also:
- Constant Field Values
-
THREAD_NAME_PREFIX
@ConfigurationProperty(name="microsphere.file-watch-service.thread-name-prefix", defaultValue="microsphere-file-watch-service", description="The thread name prefix for FileWatchService", source="system-properties") public static final java.lang.String THREAD_NAME_PREFIX
The thread name prefix , default value : "microsphere-file-watch-service-"
-
-
Constructor Detail
-
StandardFileWatchService
public StandardFileWatchService()
-
StandardFileWatchService
public StandardFileWatchService(java.util.concurrent.Executor eventHandlerExecutor)
-
StandardFileWatchService
public StandardFileWatchService(java.util.concurrent.Executor eventHandlerExecutor, java.util.concurrent.ExecutorService eventLoopExecutor)
-
-
Method Detail
-
start
public void start() throws java.lang.Exception
- Throws:
java.lang.Exception
-
watch
public void watch(java.io.File file, FileChangedListener listener, FileChangedEvent.Kind... kinds)
Description copied from interface:FileWatchService
- Specified by:
watch
in interfaceFileWatchService
- Parameters:
file
- the file or directorylistener
- onelistener
kinds
- one or morekinds of File Changed Events
, all kinds should be interested if blank
-
stop
public void stop() throws java.lang.Exception
- Throws:
java.lang.Exception
-
close
public void close() throws java.lang.Exception
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.lang.Exception
-
-