org.apache.commons.io.monitor

## Class FileAlterationMonitor

- java.lang.Object
- - org.apache.commons.io.monitor.FileAlterationMonitor

- All Implemented Interfaces:
    Runnable

      public final class FileAlterationMonitor
      extends Object
      implements Runnable

  A runnable that spawns a monitoring thread triggering any registered `FileAlterationObserver` at a specified interval.
  Since:
    2.0
  See Also:
    `FileAlterationObserver`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileAlterationMonitor()` Constructs a monitor with a default interval of 10 seconds.
    `FileAlterationMonitor(long intervalMillis)` Constructs a monitor with the specified interval.
    `FileAlterationMonitor(long interval, Collection<FileAlterationObserver> observers)` Constructs a monitor with the specified interval and collection of observers.
    `FileAlterationMonitor(long interval, FileAlterationObserver... observers)` Constructs a monitor with the specified interval and set of observers.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addObserver(FileAlterationObserver observer)` Adds a file system observer to this monitor.
    `long` | `getInterval()` Returns the interval.
    `Iterable<FileAlterationObserver>` | `getObservers()` Returns the set of `FileAlterationObserver` registered with this monitor.
    `void` | `removeObserver(FileAlterationObserver observer)` Removes a file system observer from this monitor.
    `void` | `run()` Runs this monitor.
    `void` | `setThreadFactory(ThreadFactory threadFactory)` Sets the thread factory.
    `void` | `start()` Starts monitoring.
    `void` | `stop()` Stops monitoring.
    `void` | `stop(long stopInterval)` Stops monitoring.

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

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

- - ### Constructor Detail

    - #### FileAlterationMonitor

          public FileAlterationMonitor()

      Constructs a monitor with a default interval of 10 seconds.

    - #### FileAlterationMonitor

          public FileAlterationMonitor(long intervalMillis)

      Constructs a monitor with the specified interval.
      Parameters:
        `intervalMillis` - The amount of time in milliseconds to wait between checks of the file system.

    - #### FileAlterationMonitor

          public FileAlterationMonitor(long interval,
                                       Collection<FileAlterationObserver> observers)

      Constructs a monitor with the specified interval and collection of observers.
      Parameters:
        `interval` - The amount of time in milliseconds to wait between checks of the file system.
        `observers` - The collection of observers to add to the monitor.
      Since:
        2.9.0

    - #### FileAlterationMonitor

          public FileAlterationMonitor(long interval,
                                       FileAlterationObserver... observers)

      Constructs a monitor with the specified interval and set of observers.
      Parameters:
        `interval` - The amount of time in milliseconds to wait between checks of the file system.
        `observers` - The set of observers to add to the monitor.

  - ### Method Detail

    - #### addObserver

          public void addObserver(FileAlterationObserver observer)

      Adds a file system observer to this monitor.
      Parameters:
        `observer` - The file system observer to add

    - #### getInterval

          public long getInterval()

      Returns the interval.
      Returns:
        the interval

    - #### getObservers

          public Iterable<FileAlterationObserver> getObservers()

      Returns the set of `FileAlterationObserver` registered with this monitor.
      Returns:
        The set of `FileAlterationObserver`

    - #### removeObserver

          public void removeObserver(FileAlterationObserver observer)

      Removes a file system observer from this monitor.
      Parameters:
        `observer` - The file system observer to remove

    - #### run

          public void run()

      Runs this monitor.
      Specified by:
        `run` in interface `Runnable`

    - #### setThreadFactory

          public void setThreadFactory(ThreadFactory threadFactory)

      Sets the thread factory.
      Parameters:
        `threadFactory` - the thread factory

    - #### start

          public void start()
                     throws Exception

      Starts monitoring.
      Throws:
        `Exception` - if an error occurs initializing the observer

    - #### stop

          public void stop()
                    throws Exception

      Stops monitoring.
      Throws:
        `Exception` - if an error occurs initializing the observer

    - #### stop

          public void stop(long stopInterval)
                    throws Exception

      Stops monitoring.
      Parameters:
        `stopInterval` - the amount of time in milliseconds to wait for the thread to finish. A value of zero will wait until the thread is finished (see `Thread.join(long)`).
      Throws:
        `Exception` - if an error occurs initializing the observer
      Since:
        2.1

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.