热加载Properties 工具类

项目中经常会出现更改某个配置项后,很无奈的要重启服务使其生效,因此写下此工具类。

主体思想是建立一个守护线程每隔一段时间重新加载一下配置项


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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值