I am watching a directory for incoming files (using FileAlterationObserver from apache commons).
class Example implements FileAlterationListener {
public void prepare() {
File directory = new File("/tmp/incoming");
FileAlterationObserver observer = new FileAlterationObserver(directory);
observer.addListener(this);
FileAlterationMonitor monitor = new FileAlterationMonitor(10);
monitor.addObserver(observer);
monitor.start();
// ...
}
public void handleFile(File f) {
// FIXME: this should be called when the writes that
// created the file have completed, not before
}
public void onFileCreate(File f) {
handleFile(f);
}
public void onFileChange(File f) {
handleFile(f);
}
}
The files are written in place by processes that I have no control over.
The problem I have with that code is that my callback is triggered when the File is initially created. I need it to trigger when the file has been changed and the write to the file has completed. (maybe by detecting when the file stopped changing)
What's the best way to do that?