|
@@ -24,6 +24,9 @@ import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Collections;
|
|
|
|
+import java.util.List;
|
|
import java.util.TimerTask;
|
|
import java.util.TimerTask;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
@@ -42,34 +45,59 @@ public class FileMonitoringTimerTask extends TimerTask {
|
|
static final String PROCESS_ERROR_MESSAGE =
|
|
static final String PROCESS_ERROR_MESSAGE =
|
|
"Could not process file change : ";
|
|
"Could not process file change : ";
|
|
|
|
|
|
- final private Path filePath;
|
|
|
|
|
|
+ final private List<Path> filePaths;
|
|
final private Consumer<Path> onFileChange;
|
|
final private Consumer<Path> onFileChange;
|
|
final Consumer<Throwable> onChangeFailure;
|
|
final Consumer<Throwable> onChangeFailure;
|
|
- private long lastProcessed;
|
|
|
|
|
|
+ private List<Long> lastProcessed;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Create file monitoring task to be scheduled using a standard Java {@link java.util.Timer}
|
|
|
|
- * instance.
|
|
|
|
|
|
+ * See {@link #FileMonitoringTimerTask(List, Consumer, Consumer)}.
|
|
*
|
|
*
|
|
- * @param filePath The path to the file to monitor.
|
|
|
|
- * @param onFileChange The function to call when the file has changed.
|
|
|
|
- * @param onChangeFailure The function to call when an exception is thrown during the
|
|
|
|
- * file change processing.
|
|
|
|
|
|
+ * @param filePath The file to monitor.
|
|
|
|
+ * @param onFileChange What to do when the file changes.
|
|
|
|
+ * @param onChangeFailure What to do when <code>onFileChange</code>
|
|
|
|
+ * throws an exception.
|
|
*/
|
|
*/
|
|
public FileMonitoringTimerTask(Path filePath, Consumer<Path> onFileChange,
|
|
public FileMonitoringTimerTask(Path filePath, Consumer<Path> onFileChange,
|
|
- Consumer<Throwable> onChangeFailure) {
|
|
|
|
- Preconditions.checkNotNull(filePath, "path to monitor disk file is not set");
|
|
|
|
- Preconditions.checkNotNull(onFileChange, "action to monitor disk file is not set");
|
|
|
|
|
|
+ Consumer<Throwable> onChangeFailure) {
|
|
|
|
+ this(Collections.singletonList(filePath), onFileChange, onChangeFailure);
|
|
|
|
+ }
|
|
|
|
|
|
- this.filePath = filePath;
|
|
|
|
- this.lastProcessed = filePath.toFile().lastModified();
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Create file monitoring task to be scheduled using a standard
|
|
|
|
+ * Java {@link java.util.Timer} instance.
|
|
|
|
+ *
|
|
|
|
+ * @param filePaths The path to the file to monitor.
|
|
|
|
+ * @param onFileChange The function to call when the file has changed.
|
|
|
|
+ * @param onChangeFailure The function to call when an exception is
|
|
|
|
+ * thrown during the file change processing.
|
|
|
|
+ */
|
|
|
|
+ public FileMonitoringTimerTask(List<Path> filePaths,
|
|
|
|
+ Consumer<Path> onFileChange,
|
|
|
|
+ Consumer<Throwable> onChangeFailure) {
|
|
|
|
+ Preconditions.checkNotNull(filePaths,
|
|
|
|
+ "path to monitor disk file is not set");
|
|
|
|
+ Preconditions.checkNotNull(onFileChange,
|
|
|
|
+ "action to monitor disk file is not set");
|
|
|
|
+
|
|
|
|
+ this.filePaths = new ArrayList<Path>(filePaths);
|
|
|
|
+ this.lastProcessed = new ArrayList<Long>();
|
|
|
|
+ this.filePaths.forEach(path ->
|
|
|
|
+ this.lastProcessed.add(path.toFile().lastModified()));
|
|
this.onFileChange = onFileChange;
|
|
this.onFileChange = onFileChange;
|
|
this.onChangeFailure = onChangeFailure;
|
|
this.onChangeFailure = onChangeFailure;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
- if (lastProcessed != filePath.toFile().lastModified()) {
|
|
|
|
|
|
+ int modified = -1;
|
|
|
|
+ for (int i = 0; i < filePaths.size() && modified < 0; i++) {
|
|
|
|
+ if (lastProcessed.get(i) != filePaths.get(i).toFile().lastModified()) {
|
|
|
|
+ modified = i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (modified > -1) {
|
|
|
|
+ Path filePath = filePaths.get(modified);
|
|
try {
|
|
try {
|
|
onFileChange.accept(filePath);
|
|
onFileChange.accept(filePath);
|
|
} catch (Throwable t) {
|
|
} catch (Throwable t) {
|
|
@@ -79,7 +107,7 @@ public class FileMonitoringTimerTask extends TimerTask {
|
|
LOG.error(PROCESS_ERROR_MESSAGE + filePath.toString(), t);
|
|
LOG.error(PROCESS_ERROR_MESSAGE + filePath.toString(), t);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- lastProcessed = filePath.toFile().lastModified();
|
|
|
|
|
|
+ lastProcessed.set(modified, filePath.toFile().lastModified());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|