fileloadwatcher.java

来自「用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息」· Java 代码 · 共 82 行

JAVA
82
字号
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 + =
减小字号Ctrl + -
显示快捷键?