java配置文件能reload,FileChangedReloadingStrategy 动态的刷新满载Reload配置文件

package org.java;

import java.io.File;

import org.apache.commons.configuration.PropertiesConfiguration;

import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 动态的刷新重载Reload配置文件,包括xml和property文件

*

* @author Administrator

*

*/

public class appFileChangedReloadingStrategy {

private static Log logger = LogFactory.getLog(appFileChangedReloadingStrategy.class);

//资源文件路径

private String filePath = "D://quartz.properties";

//设置文件策略更新时间为10秒

private long reloadPeriod = 10;

//Properties文件 (配置文件为Properties格式)

private PropertiesConfiguration config;

/**

* @param args

*/

public static void main(String[] args) {

appFileChangedReloadingStrategy appFileStrategy = new appFileChangedReloadingStrategy();

appFileStrategy.init();

//根据key 得到value

Object d = appFileStrategy.getString("org.quartz.plugin.shutdownhook.class");

System.out.println(d);

}

public void init() {

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.initInfo(file);

}

public void initInfo(File file) {

try {

config = new PropertiesConfiguration();

config.load(file);

//设置文件更新策略

FileChangedReloadingStrategy fs = new FileChangedReloadingStrategy();

if (this.reloadPeriod > 0) {

//设置文件策略更新时间为10秒

fs.setRefreshDelay(this.reloadPeriod);

}

//配置策略

config.setReloadingStrategy(fs);

} catch (Exception 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.getString(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 reloadPeriod.

*/

public long getReloadPeriod() {

return reloadPeriod;

}

/**

* @param reloadPeriod

*            The reloadPeriod to set.

*/

public void setReloadPeriod(long reloadPeriod) {

this.reloadPeriod = reloadPeriod;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值