用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 代码

  1. public class DefaultRealTimeXMLConfiger {
  2. private static Log logger = LogFactory.getLog(DefaultRealTimeXMLConfiger.class);
  3. private String fileName;
  4. private long reloadPeriod;
  5. private XMLConfiguration config;
  6. public void init()
  7. {
  8. String filePath = GlobalConfigerImpl.getConfDir()+"/"+fileName;
  9. logger.debug("will config with XML file["+filePath+"]");
  10. File file = new File(filePath);
  11. if (!file.exists() || !file.isFile()) {
  12. logger.error(" can't find file[" + filePath + "]");
  13. throw new IllegalArgumentException("config error! can't find file[" + filePath + "]");
  14. }
  15. this.init(file);
  16. }
  17. public void init(File file) {
  18. try {
  19. config = new XMLConfiguration(file);
  20. FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();
  21. fs.setConfiguration(config);
  22. if(this.reloadPeriod>0)
  23. {
  24. fs.setRefreshDelay(this.reloadPeriod);
  25. }
  26. config.setReloadingStrategy(fs);
  27. } catch (ConfigurationException e) {
  28. logger.error("error! configer error["+file.getPath()+"]");
  29. logger.error(e);
  30. e.printStackTrace();
  31. }
  32. }
  33. public Object getProperty(String name) {
  34. Object s = this.config.getProperty(name);
  35. return s;
  36. }
  37. public String getString(String name) {
  38. Object s = this.config.getProperty(name);
  39. String result = null;
  40. if (s != null)
  41. result = (String) s;
  42. return result;
  43. }
  44. public String[] getStringArray(String name) {
  45. String[] target = this.config.getStringArray(name);
  46. return target;
  47. }
  48. ///
  49. /* @return Returns the fileName.
  50. /*/
  51. public String getFileName() {
  52. return fileName;
  53. }
  54. ///
  55. /* @param fileName The fileName to set.
  56. /*/
  57. public void setFileName(String fileName) {
  58. this.fileName = fileName;
  59. }
  60. ///
  61. /* @return Returns the reloadPeriod.
  62. /*/
  63. public long getReloadPeriod() {
  64. return reloadPeriod;
  65. }
  66. ///
  67. /* @param reloadPeriod The reloadPeriod to set.
  68. /*/
  69. public void setReloadPeriod(long reloadPeriod) {
  70. this.reloadPeriod = reloadPeriod;
  71. }
  72. }
    异常处理

发表评论

表情图标

字体颜色: 标准深红红色橙色棕色黄色绿色橄榄青色蓝色深蓝靛蓝紫色灰色白色黑色 字体大小: 标准1 (xx-small)2 (x-small)3 (small)4 (medium)5 (large)6 (x-large)7 (xx-large) 对齐: 标准居左居中居右

提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签

您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)

steven_cheng的博客

steven_cheng

搜索本博客

最近访客 >>更多访客

Loudyn的博客

Loudyn

hepeng19861212的博客

hepeng19861212 osacar的博客

osacar

kevin_gzhz的博客

kevin_gzhz

博客分类

其他分类

存档

声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。 © 2003-2011 ITeye.com. All rights reserved. [ 京ICP证110151号 ]

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途