热加载Properties 工具类
来源:互联网 发布:java 数据结构面试题 编辑:程序博客网 时间:2024/06/05 20:49
项目中经常会出现更改某个配置项后,很无奈的要重启服务使其生效,因此写下此工具类。
主体思想是建立一个守护线程每隔一段时间重新加载一下配置项
package com.bow.utils.common;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.concurrent.TimeUnit;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;/** *该工具支持properties修改热加载 *loadFile(String fileUrl) *getProperty(String key, String defaultValue) *两方法是同步的,不能并行 * @author acer * @version C10 2016年5月16日 */public class PropertiesUtil{ private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); /** * 默认加载的properties文件 */ private static final String[] DEFAULT_PROPERTIES_URL = {"conf/business/system.properties"}; private static final int LOAD_INTERVAL_SECONDS = 120; /** * properties 所有property的容器 */ private static Properties properties = new Properties(); static { for (String url : DEFAULT_PROPERTIES_URL) { loadFile(url); } monitor(); } private static synchronized void loadFile(String fileUrl) { // class.getResourceAsStream路径用相对话是相对于本class所在位置。也可以用绝对的如/conf/business/system.properties // InputStream is = PropertiesUtil.class.getResourceAsStream(fileUrl); // 此处注意ClassLoader.getResourceAsStream只能用相对于classpath的绝对路径,并且不能已 / 开头 InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileUrl); try { properties.load(is); } catch (IOException e) { logger.error("IOException when load" + fileUrl, e); } finally { try { is.close(); } catch (IOException e) { logger.error("IOException when close inputStream " + fileUrl, e); } } } /** * 守护线程每隔LOAD_INTERVAL_SECONDS秒去加载一次properties */ private static void monitor() { Thread t = new Thread(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(LOAD_INTERVAL_SECONDS); for (String url : DEFAULT_PROPERTIES_URL) { loadFile(url); } } catch (InterruptedException e) { logger.error("properties-monitor-thread Interrupted", e); } } }, "properties-monitor-thread"); // 设置为守护线程后,用户线程结束,此线程立即中断 t.setDaemon(true); t.start(); } public static String getProperty(String key) { return getProperty(key, null); } public static synchronized String getProperty(String key, String defaultValue) { Assert.notNull(key, "key can not be null"); return properties.getProperty(key) == null ? defaultValue : properties.getProperty(key); }}
1 0
- 热加载Properties 工具类
- java加载properties工具类
- 加载properties文件工具类
- 加载properties配置文件工具类
- 加载Properties文件工具类:LoadConfig
- 加载properties文件属性的Java工具类实现
- 2016-04-22jdbc工具类优化,外部资源加载,类加载器,Properties
- JAVA Properties工具类
- JAVA Properties工具类
- Properties 工具类
- Properties工具类练习
- Properties工具类
- Properties 读取工具类
- properties工具类
- properties文件工具类
- Properties | 工具类
- 读取Properties工具类
- Properties工具类
- awk命令简介
- 当遇到ajax请求中参数较多时
- 杭电。1互素
- 最小二乘拟合
- xcode6和xcode5区别
- 热加载Properties 工具类
- CSS基础-引入方法,选择器,继承
- C++Builder 模块的学习
- Java中补码的规则
- Session的超时配置
- CodeForces 602A Two Bases(水题)
- Linux下安装postgresql,创建用户远程访问及一些简单操作
- 从头认识多线程-2.21 死锁简介
- Gym 100712H Bridges