📄 fileloadwatcher.java
字号:
package lib.commons.util;
import java.io.File;
import org.apache.commons.logging.Log;
import lib.commons.Utils;
public class FileLoadWatcher implements ThreadRunnable {
private FileLoadable fileLoader;
private long intervalMillSeconds, fileLastModified;
private boolean runFlag, running;
public FileLoadWatcher(FileLoadable fileLoader) {
this(fileLoader, 5000, true);
}
public FileLoadWatcher(FileLoadable fileLoader, long intervalMillSeconds) {
this(fileLoader, intervalMillSeconds, true);
}
public FileLoadWatcher(FileLoadable fileLoader, long intervalMillSeconds,
boolean firstLoad) {
this.fileLoader = fileLoader;
this.intervalMillSeconds = intervalMillSeconds;
if (firstLoad)
load();
}
public FileLoadable getFileLoader() {
return fileLoader;
}
public void load() {
Log log = Utils.getLog(this.getClass());
if (null != this.fileLoader
&& !Utils.StringIsNullOrEmpty(this.fileLoader.getFilePath())) {
File f = new File(this.fileLoader.getFilePath());
if (f.exists()) {
long tmp_fileLastModified = f.lastModified();
if (tmp_fileLastModified != fileLastModified) {
try {
this.fileLoader.loadFile();
fileLastModified = tmp_fileLastModified;
log.info("Load file '" + f.getAbsolutePath() + "'");
} catch (Exception ex) {
log.error("Load file '" + f.getAbsolutePath() + "':"
+ ex.getMessage(), ex);
}
}
} else {
log.debug("file is not exists");
fileLastModified = 0;
}
}
}
public void run() {
runFlag = running = true;
while (runFlag) {
load();
ThreadControlHelper.threadWaitMillSeconds(this,
intervalMillSeconds, 100);
}
running = false;
}
public void stop() {
runFlag = false;
}
public boolean isRunning() {
return running;
}
public boolean getRunFlag() {
return runFlag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -