1. 程式人生 > >java讀取系統Properties配置檔案利用執行緒實時監控配置檔案變化

java讀取系統Properties配置檔案利用執行緒實時監控配置檔案變化

package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/** 
 * 讀取實時的配置檔案資訊
 * @author frank
 * @date 2015-07-02 
 */  
public class SysProperties {

	private static Logger log = Logger.getLogger(SysProperties.class);

	private static Properties SysLocalPropObject = null;

	//配置檔案路徑
	private static String defaultPropFileName = "/config/system.properties";
	//檔案更新標識
	protected long lastModifiedData = -1;
	
	private static SysProperties instance;
	
	public static SysProperties getInstance(){
		 if(instance == null){
			 instance = new SysProperties();
		 }
		 return instance;
	}
	

	/**
	 * @description 私有構造器啟動一個執行緒實時監控配置檔案
	 */
	private SysProperties() {
		SysLocalPropObject = new Properties();
		String tempPath = this.getClass().getResource(defaultPropFileName).getFile();
		File tempFile = new File(tempPath);
		final String filePath;
		if(tempFile.exists()) {
			filePath = tempPath;
		} else {
			filePath = "system.properties";
		}
		
		final SysProperties self = this;
		File propertyFile = new File(filePath);
		if (propertyFile.exists()) reloadFile(propertyFile);

		//迴圈監控配置檔案的變化,一旦發現檔案發生變化了則重讀配置檔案
		Thread t = new Thread() {
			public void run() {
				while (true) {
					//間隔1秒
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					
					try {
						File propertyFile = new File(filePath);
						if (self.lastModifiedData != propertyFile.lastModified()) {
							self.reloadFile(propertyFile);
							self.lastModifiedData = propertyFile.lastModified();
						}
					} catch (Exception e) {

					}
				}
			}
		};
		t.start();
	}

	/** 
	 * 重新載入檔案
	 * @author frank 2015-07-02
	 * @param propertyFile
	 */ 
	private void reloadFile(File propertyFile) {
		FileInputStream inputStreamLocal = null;
		try {
			inputStreamLocal = new FileInputStream(propertyFile);
			SysLocalPropObject.load(inputStreamLocal);
		} catch (Exception e) {
			if (e instanceof FileNotFoundException) {
				log.info("No Local Properties File Found");
				SysLocalPropObject = null;
			} else {
				e.printStackTrace();
			}
		} finally {
			try {
				if (inputStreamLocal != null)
					inputStreamLocal.close();
			} catch (IOException e) {
				log.info("Exception is happened when to close file stream");
			}
		}
	}


	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @return String
	 */ 
	public String getProperty(String property) {
		String val = null;

		if (SysLocalPropObject != null)
			val = SysLocalPropObject.getProperty(property);

		return (val);

	}

	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @param defaultValue 指定預設值
	 * @return String
	 */ 
	public String getProperty(String property, String defaultValue) {
		String val = null;

		if (SysLocalPropObject != null) {
			val = SysLocalPropObject.getProperty(property, defaultValue);
		} else {
			val = defaultValue;
		}

		return (val);
	}

	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @return Integer
	 */ 
	public Integer getIntProperty(String property) {
		String val = getProperty(property);
		Integer nVal = null;
		if (val != null) {
			try {
				nVal = Integer.parseInt(val);
			} catch (Exception e) {
	
			}
		}
		return nVal;

	}

	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @param defaultValue 指定預設值
	 * @return Integer
	 */ 
	public Integer getIntProperty(String property, Integer defaultValue) {
		Integer val = getIntProperty(property);

		if (val == null) {
			val = defaultValue;
		}

		return (val);
	}
 
	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @return
	 */ 
	public Long getLongProperty(String property) {
		String val = getProperty(property);
		Long nVal = null;
		try {
			nVal = Long.parseLong(val);
		} catch (Exception e) {

		}
		return nVal;

	}

	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public Long getLongProperty(String property, Long defaultValue) {
		Long val = getLongProperty(property);

		if (val == null) {
			val = defaultValue;
		}

		return (val);
	}

	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public boolean getBooleanProperty(String property, boolean defaultValue) {
		boolean retval = false;
		String val = getProperty(property);

		if (val == null || val.equals(""))
			retval = defaultValue;
		else if (val.trim().equalsIgnoreCase("true") || val.trim().equals("1"))
			retval = true;

		return (retval);
	}
	
	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @return
	 */ 
	public Double getDoubleProperty(String property) {
		String val = getProperty(property);
		Double nVal = null;
		try {
			nVal = Double.parseDouble(val);
		} catch (Exception e) {
		}
		return nVal;
	}


	/** 
	 * 根據key獲取value
	 * @author frank 2015-07-02
	 * @param property
	 * @param defaultValue
	 * @return
	 */ 
	public Double getDoubleProperty(String property, Double defaultValue) {
		Double val = getDoubleProperty(property);
		if (val == null) {
			val = defaultValue;
		}
		return (val);
	}
}