搜ijsio 发表于 2017-1-6 11:50:13

用apache的configuration实现实时配置

apache下commons有一个configeration包,对于做配置很方便,尤其是实时热配置。可以自动监测到配置文件的更改而reload配置文件。在项目中使用所以进行了一下封装。

java 代码

[*]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;   
[*]    }   
[*]}  
页: [1]
查看完整版本: 用apache的configuration实现实时配置