Class WatchServiceListener
- java.lang.Object
-
- org.spongepowered.configurate.reference.WatchServiceListener
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public final class WatchServiceListener extends java.lang.Object implements java.lang.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(java.util.function.Function<java.nio.file.Path,ConfigurationLoader<? extends N>> loaderFunc, java.nio.file.Path path)
Create a newConfigurationReference
subscribed to FS updates.Disposable
listenToDirectory(java.nio.file.Path directory, Subscriber<java.nio.file.WatchEvent<?>> callback)
Listen to a directory.Disposable
listenToFile(java.nio.file.Path file, Subscriber<java.nio.file.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 java.io.IOException
Create a newWatchServiceListener
using a new cached thread pool executor and the default filesystem.- Returns:
- a new instance with default values
- Throws:
java.io.IOException
- if a watch service cannot be created- Since:
- 4.0.0
- See Also:
for customization
-
listenToFile
public Disposable listenToFile(java.nio.file.Path file, Subscriber<java.nio.file.WatchEvent<?>> callback) throws ConfigurateException, java.lang.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.java.lang.IllegalArgumentException
- if the provided path is a directory.- Since:
- 4.0.0
-
listenToDirectory
public Disposable listenToDirectory(java.nio.file.Path directory, Subscriber<java.nio.file.WatchEvent<?>> callback) throws ConfigurateException, java.lang.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.java.lang.IllegalArgumentException
- if the provided path is not a directory- Since:
- 4.0.0
-
listenToConfiguration
public <N extends ScopedConfigurationNode<N>> ConfigurationReference<N> listenToConfiguration(java.util.function.Function<java.nio.file.Path,ConfigurationLoader<? extends N>> loaderFunc, java.nio.file.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 java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.io.IOException
-
-