public abstract class AFileWatcher extends Object
ScheduledExecutorService
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.
Modifier | Constructor and 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 invokes
runOnChange(Iterable) once at least one file has changed. |
Modifier and Type | Method and 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.
|
protected AFileWatcher(long delay, TimeUnit unit, String name, Iterable<File> files)
runOnChange(Iterable)
once at least one file has changed.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.public boolean shutdownAndAwaitTermination(int timeout, TimeUnit unit) throws InterruptedException
timeout
- Maximum time to wait for termination.unit
- Time unit for the timeout.true
if this watcher is now terminated, false
otherwise.InterruptedException
- if interrupted while waitingExecutorService.shutdown()
,
ExecutorService.awaitTermination(long, TimeUnit)
public void shutdown()
ExecutorService.shutdown()
protected abstract void runOnChange(Iterable<File> files)
files
- A list of files that have changed.public static AFileWatcher watchSingle(long delay, TimeUnit unit, File file, String name, Runnable action) throws Exception
Please note that the action is not run initially, only when changes occur from now on.
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.Exception
- When the watcher could not be started.Copyright © 2020 XIMA MEDIA GmbH. All rights reserved.