Class WatchServiceListener

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class WatchServiceListener
    extends java.lang.Object
    implements java.lang.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
    • 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 new WatchServiceListener 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 to
        callback - 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
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException