Package io.pravega.common.io.filesystem
Class FileModificationEventWatcher
- java.lang.Object
-
- java.lang.Thread
-
- io.pravega.common.io.filesystem.FileModificationEventWatcher
-
- All Implemented Interfaces:
FileModificationMonitor,java.lang.Runnable
public class FileModificationEventWatcher extends java.lang.Thread implements FileModificationMonitor
Watches for modifications to the specified file and performs the specified action (in the form of a callback) upon modification detection. This implementation uses NIO and event-based Java WatchService to watch for any modifications to the specified file. Note that: - The specified actions may trigger slightly later than when the file is actually modified. - If there are multiple file modifications in quick succession, only the last one may trigger the specified action. - Under rare circumstances, the callback is invoked twice upon file modification. The JavaWatchServiceraises two events per modification: one for the content and second for the modification time. While there is a de-duplication logic in this class to ensure that only a single event is handled and notified to the callback, it may not work at times. If the calling code needs to be notified exactly once at all times, it should consider usingFileModificationPollingMonitorinstead. - This class won't work for a symbolic link file. This is because the JavaWatchServicedoes not raise events for modifications of such files. Consider usingFileModificationPollingMonitorinstead, when monitoring modifications to such files.
-
-
Constructor Summary
Constructors Constructor Description FileModificationEventWatcher(@NonNull java.nio.file.Path fileToWatch, @NonNull java.util.function.Consumer<java.nio.file.WatchEvent<?>> callback)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidrun()voidstartMonitoring()Start monitoring.voidstopMonitoring()Stop monitoring.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Constructor Detail
-
FileModificationEventWatcher
public FileModificationEventWatcher(@NonNull @NonNull java.nio.file.Path fileToWatch, @NonNull @NonNull java.util.function.Consumer<java.nio.file.WatchEvent<?>> callback) throws java.io.FileNotFoundExceptionCreates a new instance.- Parameters:
fileToWatch- the file to watchcallback- the callback to invoke when a modification to thefileToWatchis detected- Throws:
java.lang.NullPointerException- if eitherfileToWatchorcallbackis nulljava.nio.file.InvalidPathException- iffileToWatchis invalidjava.io.FileNotFoundException- when a file at specified pathfileToWatchdoes not exist
-
-
Method Detail
-
run
public void run()
- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
startMonitoring
public void startMonitoring()
Description copied from interface:FileModificationMonitorStart monitoring.- Specified by:
startMonitoringin interfaceFileModificationMonitor
-
stopMonitoring
public void stopMonitoring()
Description copied from interface:FileModificationMonitorStop monitoring.- Specified by:
stopMonitoringin interfaceFileModificationMonitor
-
-