Class WatchServiceListener
java.lang.Object
org.spongepowered.configurate.reference.WatchServiceListener
- All Implemented Interfaces:
AutoCloseable
A wrapper around NIO's
WatchService
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 the
WatchEvent
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 ClassesModifier and TypeClassDescriptionstatic final class
Set the parameters needed to create aWatchServiceListener
. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.listenToDirectory
(Path directory, Subscriber<WatchEvent<?>> callback) Listen to a directory.listenToFile
(Path file, Subscriber<WatchEvent<?>> callback) Listen for changes to a specific file or directory.
-
Method Details
-
builder
Returns a new builder for a WatchServiceListener to create a customized listener.- Returns:
- a new builder
- Since:
- 4.0.0
-
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- Since:
- 4.0.0
- See Also:
-
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 ConfigurateExceptionCreate 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
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-