刷新Reload你的properties配置文件
Apache Commons Configuration是一个操作配置文件的工具包,它也支持动态的刷新重载Reload配置文件,包括xml和property文件。通过FileChangedReloadingStrategy刷新策略来支持动态的重载属性配置文件。
- public class DefaultRealTimeXMLConfiger {
- private static Log logger = LogFactory.getLog(DefaultRealTimeXMLConfiger.class);
- private String fileName;
- private long reloadPeriod;
- private XMLConfiguration config;
- public void init()
- {
- String filePath = GlobalConfigerImpl.getConfDir()+"/"+fileName;
- logger.debug("will config with XML file["+filePath+"]");
- File file = new File(filePath);
- if (!file.exists() || !file.isFile()) {
- logger.error(" can't find file[" + filePath + "]");
- throw new IllegalArgumentException("config error! can't find file[" + filePath + "]");
- }
- this.init(file);
- }
- public void init(File file) {
- try {
- config = new XMLConfiguration(file);
- FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();
- fs.setConfiguration(config);
- if(this.reloadPeriod>0)
- {
- fs.setRefreshDelay(this.reloadPeriod);
- }
- config.setReloadingStrategy(fs);
- } catch (ConfigurationException e) {
- logger.error("error! configer error["+file.getPath()+"]");
- logger.error(e);
- e.printStackTrace();
- }
- }
- public Object getProperty(String name) {
- Object s = this.config.getProperty(name);
- return s;
- }
- public String getString(String name) {
- Object s = this.config.getProperty(name);
- String result = null;
- if (s != null)
- result = (String) s;
- return result;
- }
- public String[] getStringArray(String name) {
- String[] target = this.config.getStringArray(name);
- return target;
- }
- /**
- * @return Returns the fileName.
- */
- public String getFileName() {
- return fileName;
- }
- /**
- * @param fileName The fileName to set.
- */
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- /**
- * @return Returns the reloadPeriod.
- */
- public long getReloadPeriod() {
- return reloadPeriod;
- }
- /**
- * @param reloadPeriod The reloadPeriod to set.
- */
- public void setReloadPeriod(long reloadPeriod) {
- this.reloadPeriod = reloadPeriod;
- }
- }