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's
WatchService
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 the WatchEvent
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
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 Details
-
builder
Create a new builder for a WatchServiceListener to create a customized listener- Returns:
- A builder
-
create
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, IllegalArgumentExceptionListen 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, IllegalArgumentExceptionListen 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
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-