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'sWatchService
that 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
Subscribers
that take theWatchEvent
as 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 class
WatchServiceListener.Builder
Set the parameters needed to create aWatchServiceListener
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static WatchServiceListener.Builder
builder()
Create a new builder for a WatchServiceListener to create a customized listenervoid
close()
static WatchServiceListener
create()
Create a newWatchServiceListener
using a new cached thread pool executor and the default filesystem.<N extends ConfigurationNode>
ConfigurationReference<N>listenToConfiguration(Function<Path,ConfigurationLoader<? extends N>> loaderFunc, Path path)
Disposable
listenToDirectory(Path directory, Subscriber<WatchEvent<?>> callback)
Listen to a directory.Disposable
listenToFile(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 newWatchServiceListener
using 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
Disposable
that 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
Disposable
that 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:
close
in interfaceAutoCloseable
- Throws:
IOException
-
-