ITKeyword,专注技术干货聚合推荐

注册 | 登录

java读取系统Properties配置文件利用线程实时监控配置文件变化

u012104497 分享于 2015-07-02

推荐:java 读取 properties 配置文件 备忘

/**  *  *  创建人:houying  *  创建日期:2009-4-28 上午09:27:53  *  MSN:  *  Email:  *  */ package com.properties; import java.util.*; import java.io.

2018阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=zo93kaue

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);
	}
}

推荐:Java Properties 类读取配置文件信息

  在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们连接本地的数据库那么 I

package util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;import org.apach

相关阅读排行


用户评论

游客

相关内容推荐

阿里云RDS

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。