FileChangeWatcher (Apache ZooKeeper - Server 3.7.0 API)
Date: 2021-03-17

org.apache.zookeeper.common

## Class FileChangeWatcher

- java.lang.Object
- - org.apache.zookeeper.common.FileChangeWatcher

-     public final class FileChangeWatcher
      extends Object

  Instances of this class can be used to watch a directory for file changes. When a file is added to, deleted from, or is modified in the given directory, the callback provided by the user will be called from a background thread. Some things to keep in mind:
  - The callback should be thread-safe.
  - Changes that happen around the time the thread is started may be missed.
  - There is a delay between a file changing and the callback firing.
  - The watch is not recursive - changes to subdirectories will not trigger a callback.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `FileChangeWatcher.State`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileChangeWatcher(Path dirPath, Consumer<WatchEvent<?>> callback)` Creates a watcher that watches `dirPath` and invokes `callback` on changes.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `FileChangeWatcher.State` | `getState()` Returns the current `FileChangeWatcher.State`.
    `void` | `start()` Tells the background thread to start.
    `void` | `stop()` Tells the background thread to stop.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FileChangeWatcher

          public FileChangeWatcher(Path dirPath,
                                   Consumer<WatchEvent<?>> callback)
                            throws IOException

      Creates a watcher that watches `dirPath` and invokes `callback` on changes.
      Parameters:
        `dirPath` - the directory to watch.
        `callback` - the callback to invoke with events. `event.kind()` will return the type of event, and `event.context()` will return the filename relative to `dirPath`.
      Throws:
        `IOException` - if there is an error creating the WatchService.

  - ### Method Detail

    - #### getState

          public FileChangeWatcher.State getState()

      Returns the current `FileChangeWatcher.State`.
      Returns:
        the current state.

    - #### start

          public void start()

      Tells the background thread to start. Does not wait for it to be running. Calling this method more than once has no effect.

    - #### stop

          public void stop()

      Tells the background thread to stop. Does not wait for it to exit.

Copyright © 2008–2021 The Apache Software Foundation. All rights reserved.