Package engineering.swat.watch
Class Watch
- java.lang.Object
-
- engineering.swat.watch.Watch
-
public class Watch extends Object
Watch a path for changes.
It will avoid common errors using the raw apis, and will try to use the most native api where possible.
Note, there are differences per platform that cannot be avoided, please review the readme of the library.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Watchbuild(Path path, WatchScope scope)Watch a path for updates, optionally also get events for its children/descendantsWatchon(WatchEventListener listener)Convenience variant ofon(Consumer), which allows you to only respond to certain eventsWatchon(Consumer<WatchEvent> eventHandler)Callback that gets executed for every event.WatchonOverflow(Approximation whichFiles)Optionally configure which regular files/directories in the scope of the watch an approximation of synthetic events (of kindsWatchEvent.Kind.CREATED,WatchEvent.Kind.MODIFIED, and/orWatchEvent.Kind.DELETED) should be issued when an overflow event happens.ActiveWatchstart()Start watch the path for events.WatchwithExecutor(Executor callbackHandler)Optionally configure the executor in which theon(Consumer)callbacks are scheduled.
-
-
-
Method Detail
-
build
public static Watch build(Path path, WatchScope scope)
Watch a path for updates, optionally also get events for its children/descendants- Parameters:
path- which absolute path to monitor, can be a file or a directory, but has to be absolutescope- for directories you can also choose to monitor it's direct children or all it's descendants- Returns:
- watch builder that can be further configured and then started
- Throws:
IllegalArgumentException- in case a path is not supported (in relation to the scope)
-
on
public Watch on(Consumer<WatchEvent> eventHandler)
Callback that gets executed for every event. Can get called quite a bit, so be careful what happens here. Use thewithExecutor(Executor)function to influence the sequencing of these events. By default they can arrive in parallel.- Parameters:
eventHandler- a callback that handles the watch event, will be called once per event.- Returns:
this(to support method chaining)
-
on
public Watch on(WatchEventListener listener)
Convenience variant ofon(Consumer), which allows you to only respond to certain events- Parameters:
listener- gets executed on every event the watch produced- Returns:
this(to support method chaining)
-
withExecutor
public Watch withExecutor(Executor callbackHandler)
Optionally configure the executor in which theon(Consumer)callbacks are scheduled. Make sure to consider the termination of the threadpool, it should be after the close of the active watch.- Parameters:
callbackHandler- worker pool to use- Returns:
- this for optional method chaining
-
onOverflow
public Watch onOverflow(Approximation whichFiles)
Optionally configure which regular files/directories in the scope of the watch an approximation of synthetic events (of kindsWatchEvent.Kind.CREATED,WatchEvent.Kind.MODIFIED, and/orWatchEvent.Kind.DELETED) should be issued when an overflow event happens. If not defined before this watcher is started, theApproximation.ALLapproach will be used.- Parameters:
whichFiles- Constant to indicate for which regular files/directories to approximate- Returns:
- This watcher for optional method chaining
-
start
public ActiveWatch start() throws IOException
Start watch the path for events.- Returns:
- a subscription for the watch, when closed, new events will stop being registered to the worker pool.
- Throws:
IOException- in case the starting of the watcher caused an underlying IO exceptionIllegalStateException- the watchers is not configured correctly (for example, missingon(Consumer), or a watcher is started twice)
-
-