Class WatchServiceListener
- java.lang.Object
-
- ninja.leaping.configurate.reference.WatchServiceListener
-
- All Implemented Interfaces:
AutoCloseable
public class WatchServiceListener extends Object implements AutoCloseable
A wrapper around NIO'sWatchServicethat uses the provided event loop to poll for changes, and calls listeners once an event occurs.Some deduplication is performed because Windows can be fairly spammy with its events, so one callback may receive multiple events at one time.
Callback functions are
Subscribersthat take theWatchEventas their parameter.Listening to a directory provides updates on the directory's immediate children, but does not
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classWatchServiceListener.BuilderSet the parameters needed to create aWatchServiceListener.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static WatchServiceListener.Builderbuilder()Create a new builder for a WatchServiceListener to create a customized listenervoidclose()static WatchServiceListenercreate()Create a newWatchServiceListenerusing a new cached thread pool executor and the default filesystem.<N extends ConfigurationNode>
ConfigurationReference<N>listenToConfiguration(Function<Path,ConfigurationLoader<? extends N>> loaderFunc, Path path)DisposablelistenToDirectory(Path directory, Subscriber<WatchEvent<?>> callback)Listen to a directory.DisposablelistenToFile(Path file, Subscriber<WatchEvent<?>> callback)Listen for changes to a specific file or directory.
-
-
-
Method Detail
-
builder
public static WatchServiceListener.Builder builder()
Create a new builder for a WatchServiceListener to create a customized listener- Returns:
- A builder
-
create
public static WatchServiceListener create() throws IOException
Create a newWatchServiceListenerusing a new cached thread pool executor and the default filesystem.- Returns:
- A new instance with default values
- Throws:
IOException- If a watch service cannot be created- See Also:
for customization
-
listenToFile
public Disposable listenToFile(Path file, Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException
Listen for changes to a specific file or directory.- Parameters:
file- The path of the file or directory to listen for changes on.callback- A callback function that will be called when changes are made. If return value is false, we will stop monitoring for changes.- Returns:
- A
Disposablethat can be used to cancel this subscription - Throws:
IOException- if a filesystem error occurs.IllegalArgumentException- if the provided path is a directory.
-
listenToDirectory
public Disposable listenToDirectory(Path directory, Subscriber<WatchEvent<?>> callback) throws IOException, IllegalArgumentException
Listen to a directory. Callbacks will receive events both for the directory and for its contents.- Parameters:
directory- The directory to listen tocallback- A callback function that will be called when changes are made. If return value is false, we will stop monitoring for changes.- Returns:
- A
Disposablethat can be used to cancel this subscription - Throws:
IOException- When an error occurs registering with the underlying watch service.IllegalArgumentException- If the provided path is not a directory
-
listenToConfiguration
public <N extends ConfigurationNode> ConfigurationReference<N> listenToConfiguration(Function<Path,ConfigurationLoader<? extends N>> loaderFunc, Path path) throws IOException
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
-