Package de.xima.fc.config
Class AFileWatcher
- java.lang.Object
-
- de.xima.fc.config.AFileWatcher
-
public abstract class AFileWatcher extends Object
A watcher that runs an action each time at least one of the watched files have changed. This implementation uses aScheduledExecutorService
to poll the files for changes in fixed intervals.You may either use one of the static methods of this class to watch a file; or extend this class and override the runOnChange(Iterable) method.
Please note that the action is not run initially, only when changes occur from now on.
- Author:
- XIMA MEDIA GmbH
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AFileWatcher(long delay, TimeUnit unit, String name, Iterable<File> files)
Creates a new file watcher that periodically checks the given files for changes; and invokesrunOnChange(Iterable)
once at least one file has changed.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract void
runOnChange(Iterable<File> files)
An action that is called when one or more of the watched files have changed.void
shutdown()
Requests this watcher to terminate, and returns immediately.boolean
shutdownAndAwaitTermination(int timeout, TimeUnit unit)
Blocks until this watcher terminates, or the timeout is reached.static AFileWatcher
watchSingle(long delay, TimeUnit unit, File file, String name, Runnable action)
Watches a single file for changes via polling for changes in fixed intervals.
-
-
-
Constructor Detail
-
AFileWatcher
protected AFileWatcher(long delay, TimeUnit unit, String name, Iterable<File> files)
Creates a new file watcher that periodically checks the given files for changes; and invokesrunOnChange(Iterable)
once at least one file has changed.- Parameters:
delay
- Delay between checks.unit
- Time unit of the delay.name
- A name for this file watcher, also used for naming the threads.files
- File to check for changes.
-
-
Method Detail
-
shutdownAndAwaitTermination
public boolean shutdownAndAwaitTermination(int timeout, TimeUnit unit) throws InterruptedException
Blocks until this watcher terminates, or the timeout is reached.- Parameters:
timeout
- Maximum time to wait for termination.unit
- Time unit for the timeout.- Returns:
true
if this watcher is now terminated,false
otherwise.- Throws:
InterruptedException
- if interrupted while waiting- See Also:
ExecutorService.shutdown()
,ExecutorService.awaitTermination(long, TimeUnit)
-
shutdown
public void shutdown()
Requests this watcher to terminate, and returns immediately.- See Also:
ExecutorService.shutdown()
-
runOnChange
protected abstract void runOnChange(Iterable<File> files)
An action that is called when one or more of the watched files have changed.- Parameters:
files
- A list of files that have changed.
-
watchSingle
public static AFileWatcher watchSingle(long delay, TimeUnit unit, File file, String name, Runnable action) throws Exception
Watches a single file for changes via polling for changes in fixed intervals. The given action is run each time the file has changed.Please note that the action is not run initially, only when changes occur from now on.
- Parameters:
delay
- Duration between pollings.unit
- Time unit for the given duration.file
- A file to watch for changes.name
- A name for the action, also used for naming the threads.action
- An action to run each time the file has changed.- Returns:
- A watcher instance for stopping the watch.
- Throws:
Exception
- When the watcher could not be started.
-
-