用apache的configuration实现实时配置
Posted on用apache的configuration实现实时配置 - steven_cheng - ITeye技术网站
steven_cheng
永久域名 http://steven-cheng.iteye.com
2006-03-24
用apache的configuration实现实时配置
关键字: java 开源 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 (xx-small)2 (x-small)3 (small)4 (medium)5 (large)6 (x-large)7 (xx-large) 对齐: 标准居左居中居右
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签
您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)
steven_cheng
搜索本博客
最近访客 >>更多访客
博客分类
- 全部博客 (8)
- 默认类别 (0)
- java (6)
- 单元测试和TDD (0)
- freemarker (1)
- linux (1)
-
我的留言簿 >>更多留言
steven_cheng 写道restlet那片原文是你的吗,如果是给个原文连接。 ... -- by whaosoft
其他分类
存档
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。 © 2003-2011 ITeye.com. All rights reserved. [ 京ICP证110151号 ]