org.apache.commons.io.monitor
Class FileAlterationMonitor

java.lang.Object
  extended by org.apache.commons.io.monitor.FileAlterationMonitor
All Implemented Interfaces:
java.lang.Runnable

public final class FileAlterationMonitor
extends java.lang.Object
implements java.lang.Runnable

A runnable that spawns a monitoring thread triggering any registered FileAlterationObserver at a specified interval.

Since:
Commons IO 2.0
Version:
$Id: FileAlterationMonitor.java 1080934 2011-03-12 13:54:58Z niallp $
See Also:
FileAlterationObserver

Field Summary
private  long interval
           
private  java.util.List<FileAlterationObserver> observers
           
private  boolean running
           
private  java.lang.Thread thread
           
private  java.util.concurrent.ThreadFactory threadFactory
           
 
Constructor Summary
FileAlterationMonitor()
          Construct a monitor with a default interval of 10 seconds.
FileAlterationMonitor(long interval)
          Construct a monitor with the specified interval.
FileAlterationMonitor(long interval, FileAlterationObserver... observers)
          Construct a monitor with the specified interval and set of observers.
 
Method Summary
 void addObserver(FileAlterationObserver observer)
          Add a file system observer to this monitor.
 long getInterval()
          Return the interval.
 java.lang.Iterable<FileAlterationObserver> getObservers()
          Returns the set of FileAlterationObserver registered with this monitor.
 void removeObserver(FileAlterationObserver observer)
          Remove a file system observer from this monitor.
 void run()
          Run.
 void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
          Set the thread factory.
 void start()
          Start monitoring.
 void stop()
          Stop monitoring.
 void stop(long stopInterval)
          Stop monitoring.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

interval

private final long interval

observers

private final java.util.List<FileAlterationObserver> observers

thread

private java.lang.Thread thread

threadFactory

private java.util.concurrent.ThreadFactory threadFactory

running

private volatile boolean running
Constructor Detail

FileAlterationMonitor

public FileAlterationMonitor()
Construct a monitor with a default interval of 10 seconds.


FileAlterationMonitor

public FileAlterationMonitor(long interval)
Construct a monitor with the specified interval.

Parameters:
interval - The amount of time in miliseconds to wait between checks of the file system

FileAlterationMonitor

public FileAlterationMonitor(long interval,
                             FileAlterationObserver... observers)
Construct a monitor with the specified interval and set of observers.

Parameters:
interval - The amount of time in miliseconds to wait between checks of the file system
observers - The set of observers to add to the monitor.
Method Detail

getInterval

public long getInterval()
Return the interval.

Returns:
the interval

setThreadFactory

public void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Set the thread factory.

Parameters:
threadFactory - the thread factory

addObserver

public void addObserver(FileAlterationObserver observer)
Add a file system observer to this monitor.

Parameters:
observer - The file system observer to add

removeObserver

public void removeObserver(FileAlterationObserver observer)
Remove a file system observer from this monitor.

Parameters:
observer - The file system observer to remove

getObservers

public java.lang.Iterable<FileAlterationObserver> getObservers()
Returns the set of FileAlterationObserver registered with this monitor.

Returns:
The set of FileAlterationObserver

start

public void start()
           throws java.lang.Exception
Start monitoring.

Throws:
java.lang.Exception - if an error occurs initializing the observer

stop

public void stop()
          throws java.lang.Exception
Stop monitoring.

Throws:
java.lang.Exception - if an error occurs initializing the observer

stop

public void stop(long stopInterval)
          throws java.lang.Exception
Stop 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:
java.lang.Exception - if an error occurs initializing the observer
Since:
Commons IO 2.1

run

public void run()
Run.

Specified by:
run in interface java.lang.Runnable


Copyright (c) 2002-2011 Apache Software Foundation