Package com.mastfrog.util.file
Class WatchManager
java.lang.Object
com.mastfrog.util.file.WatchManager
Manages file system watches, centralizing management of watch keys, and
enabling and disabling notifications; uses a single thread and a set of
polling intervals, and timeouts to ensure all watches are fairly serviced
with fixed overhead.
- Author:
- Tim Boudreau
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAllows for recursive watching on folders and files. -
Constructor Summary
ConstructorsConstructorDescriptionWatchManager(Duration perKeyWait, Duration maxTimePer, Duration interval) WatchManager(ScheduledExecutorService executor, long perKeyWait, long maxTimePer, long interval) WatchManager(ScheduledExecutorService executor, Duration perKeyWait, Duration maxTimePer, Duration interval) Create a watch manager. -
Method Summary
Modifier and TypeMethodDescriptionbooleanisEmpty()Determine if there are no active listeners on any files.booleanpause(boolean pause) Pause notification of file changes - no polling runs will occur; if a polling run is currently occurring, it will complete.recursiveWatch(Path root, int maxDepth, BiConsumer<Path, WatchEvent.Kind<?>> consumer, WatchEvent.Kind<?> kind, WatchEvent.Kind<?>... more) Create a new recursive watcher, already attached.booleanshutdown()Shut down this WatchManager, closing all watch keys.toString()voidunwatch(Path target, BiConsumer<Path, WatchEvent.Kind<?>> c) Stop watching a file or folder; if the passed consumer is null, this will unwatch for all listeners on the passed file.voidwatch(Path target, BiConsumer<Path, WatchEvent.Kind<?>> c, WatchEvent.Kind<?> oneKind, WatchEvent.Kind<?>... more) Watch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.voidwatch(Path target, BiConsumer<Path, WatchEvent.Kind<?>> c, Set<WatchEvent.Kind<?>> kinds) Watch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.
-
Constructor Details
-
WatchManager
public WatchManager(ScheduledExecutorService executor, Duration perKeyWait, Duration maxTimePer, Duration interval) Create a watch manager.- Parameters:
executor- The executor to poll and invoke callbacks inperKeyWait- The amount of time to wait for events on each key - if zero, polling will continue with no delaymaxTimePer- The amount of time to spend on any given key before moving on to the next one, to avoid favoring one busy folderinterval- The interval between polling runs (starts with the end of the previous polling run, not including the time that took)
-
WatchManager
-
WatchManager
public WatchManager() -
WatchManager
public WatchManager(ScheduledExecutorService executor, long perKeyWait, long maxTimePer, long interval)
-
-
Method Details
-
pause
public boolean pause(boolean pause) Pause notification of file changes - no polling runs will occur; if a polling run is currently occurring, it will complete.- Parameters:
pause- If true, pause- Returns:
- if the paused state changed
-
shutdown
public boolean shutdown()Shut down this WatchManager, closing all watch keys.- Returns:
- true if the state was changed
-
watch
public void watch(Path target, BiConsumer<Path, WatchEvent.Kind<?>> c, WatchEvent.Kind<?> oneKind, WatchEvent.Kind<?>... more) throws IOExceptionWatch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.Note that the consumer is weakly referenced in order that watch keys can be cleaned up if the listener is garbage collected.
- Parameters:
target- The target file or folderc- The consumeroneKind- The first kindmore- Additional kinds- Throws:
IOException- If something goes wrong
-
watch
public void watch(Path target, BiConsumer<Path, WatchEvent.Kind<?>> c, Set<WatchEvent.Kind<?>> kinds) throws IOExceptionWatch a file or folder; if the passed target is a file, only notifications for that file will be delivered; if a folder, notifications for any file or folder within that directory (folders may notify they were modified when a file is created or changes within them) will be delivered.Note that the consumer is weakly referenced in order that watch keys can be cleaned up if the listener is garbage collected.
- Parameters:
target- The target file or folderc- The consumerkinds- The kinds to listen to (must be non empty)- Throws:
IOException- If something goes wrong
-
unwatch
Stop watching a file or folder; if the passed consumer is null, this will unwatch for all listeners on the passed file.- Parameters:
target- The file or folderc- Optionally, a specific consumer to detach
-
isEmpty
public boolean isEmpty()Determine if there are no active listeners on any files.- Returns:
- true if there are none
-
toString
-
recursiveWatch
public WatchManager.RecursiveWatcher recursiveWatch(Path root, int maxDepth, BiConsumer<Path, WatchEvent.Kind<?>> consumer, WatchEvent.Kind<?> kind, WatchEvent.Kind<?>... more) throws IOExceptionCreate a new recursive watcher, already attached.- Parameters:
root- The root foldermaxDepth- The depth to which to listen for file changes (keep as small as possible)consumer- The consumerkind- The first kind of event to watch formore- Other kinds of events to watch for- Returns:
- A new watcher
- Throws:
IOException
-