Package com.helger.io.watchdir
Class WatchDir
java.lang.Object
com.helger.io.watchdir.WatchDir
- All Implemented Interfaces:
AutoCloseable
Generic directory watching service using the default JDK
WatchService
class.- Since:
- 9.0.0
- Author:
- Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NonNull com.helger.base.callback.CallbackList<IWatchDirCallback> voidclose()Close the watch directory service and stop processing.static @NonNull WatchDircreateAsyncRunningWatchDir(@NonNull Path aDir, boolean bRecursive, @NonNull IWatchDirCallback aCallback) Static factory method to create a simpleWatchDirinstance that already spawned an Thread to listen.@NonNull PathbooleanCheck if processing is active.booleanvoidProcess all events for keys queued to the watcher.voidrunAsync()Call this method to process events.@NonNull ThreadCall this method to process events.voidStop processing, ifprocessEvents()is active.
-
Constructor Details
-
WatchDir
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 Details
-
callbacks
@ReturnsMutableObject public @NonNull com.helger.base.callback.CallbackList<IWatchDirCallback> callbacks()- Returns:
- The modifiable callback list. Never
null.
-
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
Close 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
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
public void runAsync()Call this method to process events. This method creates a background thread than runsprocessEvents()and performs the heavy lifting. -
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
@ReturnsMutableCopy public static @NonNull 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.
-