Class WatchServiceListener
- java.lang.Object
-
- org.spongepowered.configurate.reference.WatchServiceListener
-
- All Implemented Interfaces:
AutoCloseable
public final class WatchServiceListener extends Object implements AutoCloseable
A wrapper around NIO'sWatchService
that uses the provided watch key 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 listen recursively.
- Since:
- 4.0.0
-
-
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()
Returns a new builder for a WatchServiceListener to create a customized listener.void
close()
static WatchServiceListener
create()
Create a newWatchServiceListener
using a new cached thread pool executor and the default filesystem.<N extends ScopedConfigurationNode<N>>
ConfigurationReference<N>listenToConfiguration(Function<Path,ConfigurationLoader<? extends N>> loaderFunc, Path path)
Create a newConfigurationReference
subscribed to FS updates.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()
Returns a new builder for a WatchServiceListener to create a customized listener.- Returns:
- a new builder
- Since:
- 4.0.0
-
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- Since:
- 4.0.0
- See Also:
for customization
-
listenToFile
public Disposable listenToFile(Path file, Subscriber<WatchEvent<?>> callback) throws ConfigurateException, 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 subscriber that will be notified when changes occur.- Returns:
- a
Disposable
that can be used to cancel this subscription - Throws:
ConfigurateException
- if a filesystem error occurs.IllegalArgumentException
- if the provided path is a directory.- Since:
- 4.0.0
-
listenToDirectory
public Disposable listenToDirectory(Path directory, Subscriber<WatchEvent<?>> callback) throws ConfigurateException, 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 subscriber that will be notified when changes occur.- Returns:
- a
Disposable
that can be used to cancel this subscription - Throws:
ConfigurateException
- when an error occurs registering with the underlying watch service.IllegalArgumentException
- if the provided path is not a directory- Since:
- 4.0.0
-
listenToConfiguration
public <N extends ScopedConfigurationNode<N>> ConfigurationReference<N> listenToConfiguration(Function<Path,ConfigurationLoader<? extends N>> loaderFunc, Path path) throws ConfigurateException
Create a newConfigurationReference
subscribed to FS updates.- Type Parameters:
N
- node type- Parameters:
loaderFunc
- function that will create a new loaderpath
- path to to for changes- Returns:
- new reference
- Throws:
ConfigurateException
- if unable to complete an initial load of the configuration.- Since:
- 4.0.0
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-
-