Package com.helger.commons.io.watchdir
Class WatchDir
- java.lang.Object
-
- com.helger.commons.io.watchdir.WatchDir
-
- All Implemented Interfaces:
AutoCloseable
public class WatchDir extends Object implements AutoCloseable
Generic directory watching service using the default JDKWatchServiceclass.- Since:
- 9.0.0
- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CallbackList<IWatchDirCallback>callbacks()voidclose()Close the watch directory service and stop processing.static WatchDircreateAsyncRunningWatchDir(Path aDir, boolean bRecursive, IWatchDirCallback aCallback)Static factory method to create a simpleWatchDirinstance that already spawned an Thread to listen.PathgetStartDirectory()booleanisProcessing()Check if processing is active.booleanisRecursive()voidprocessEvents()Process all events for keys queued to the watcher.voidrunAsync()Call this method to process events.ThreadrunAsyncAndReturn()Call this method to process events.voidstopProcessing()Stop processing, ifprocessEvents()is active.
-
-
-
Constructor Detail
-
WatchDir
public WatchDir(@Nonnull Path aDir, boolean bRecursive) throws IOException
Creates a WatchService and registers the given directory- Parameters:
aDir- The directory to be watched. May not benull.bRecursive-trueto watch the directory recursive,falseto watch just this directory.- Throws:
IOException- In case something goes wrong.
-
-
Method Detail
-
callbacks
@Nonnull @ReturnsMutableObject public CallbackList<IWatchDirCallback> callbacks()
- Returns:
- The modifiable callback list. Never
null.
-
getStartDirectory
@Nonnull public Path getStartDirectory()
- Returns:
- The start directory as specified in the constructor. Never
null.
-
isRecursive
public boolean isRecursive()
- Returns:
trueif this is a recursive listener,falseif not.
-
close
public void close() throws IOExceptionClose the watch directory service and stop processing.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
stopProcessing
public void stopProcessing()
Stop processing, ifprocessEvents()is active. This method is automatically called inclose().
-
isProcessing
public boolean isProcessing()
Check if processing is active.- Returns:
trueif event processing is active,falseif not.- See Also:
processEvents(),stopProcessing(),close()
-
processEvents
public void processEvents()
Process all events for keys queued to the watcher. CallstopProcessing()orclose()to stop processing within a reasonable time. This method should run in a separate thread, as it contains an infinite loop! Usually you don't call this method manually.- See Also:
runAsync()
-
runAsync
public void runAsync()
Call this method to process events. This method creates a background thread than runsprocessEvents()and performs the heavy lifting.
-
runAsyncAndReturn
@Nonnull public Thread runAsyncAndReturn()
Call this method to process events. This method creates a background thread than runsprocessEvents()and performs the heavy lifting.- Returns:
- The created
Threadthat can also be stopped again if not needed anymore. - Since:
- 10.1.5
-
createAsyncRunningWatchDir
@Nonnull @ReturnsMutableCopy public static WatchDir createAsyncRunningWatchDir(@Nonnull Path aDir, boolean bRecursive, @Nonnull IWatchDirCallback aCallback) throws IOException
Static factory method to create a simpleWatchDirinstance that already spawned an Thread to listen. To close the thread call theclose()method.- Parameters:
aDir- The directory to be watched. May not benull.bRecursive-trueto watch the directory recursive,falseto watch just this directory.aCallback- The callback to be invoked if something changed. May not benull.- Returns:
- The newly created
WatchDirinstance and nevernull. - Throws:
IOException- In case something goes wrong.
-
-