JAVA实现热加载配置文件(不重启服务器加载配置文件)

通过文件的当前时间和上次修改时间做比较,判断是否需要重新读取文件,从而实现不重启服务器获取修改后的参数

package com.shp.util;
 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

public class LoadConfigUtil {
    public static Properties properties;
    public static File file;
    public static long lastModifyTime;
    
    //服务器启动时加载配置文件到内存
    static {
        LoadConfigUtil.load("fileName");
    }

    /**
     * 读取配置文件,并加载属性列表到Properties对象中 记录文件的最后一次修改时间
     * @param pathName 文件的路径
     */
    public static void load(String pathName) {
        properties = new Properties();
        FileInputStream fis = null;
        file = new File(pathName);
        try {
            fis = new FileInputStream(file);
            properties.load(fis);
            //获取文件的最后更新时间
            lastModifyTime = file.lastModified();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取配置文件所有的value
     * 判断文件是否修改过, 是,重新读取配置文件,并返回 否,从Properties中获取返回
     * @param fileName 配置文件路径
     * @return
     */
    public static String getAllValues(String fileName) {
        file = new File(fileName);
        //判断文件是否修改过
        if (file.lastModified() > lastModifyTime) {
            System.out.println("file has been modified , need to reload!");
            load(fileName);
        }
        Collection<Object> values = properties.values();
        String v = values.toString();
        return v;
    }
    
    /**
     * 获取配置文件 key的值
     * 判断文件是否修改过, 是,重新读取配置文件,并返回 否,从Properties中获取返回
     * @param key key的名字
     * @return
     */
    public static String get(String key) {
        file = new File("配置文件路径");
        //判断文件是否修改过
        if (file.lastModified() > lastModifyTime) {
            System.out.println("file has been modified , need to reload!");
            load("pathName");
        }
        return properties.getProperty(key);
    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

用Quartz,根据设定的时间间隔来定时检查配置文件是否被修改,如被修改,重新读取

package com.shp.util;
 
import java.util.Date;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.shp.test.watchService.CronJob;
 
public class RAMQuartzUtil {
    
    public static void startTask(String key,String value) throws SchedulerException {
    //1.创建Scheduler的工厂
    SchedulerFactory  sf = new StdSchedulerFactory();
    //2.从工厂中获取调度器实例
    Scheduler scheduler = sf.getScheduler();
    //3.创建JobDetail
    JobDetail jd=JobBuilder.newJob(CronJob.class).withDescription("this is ram job").withIdentity("ramjob", "ramgroup").build();
    //存放附加信息到jobDataMap中,便于在excute方法中获取
      jd.getJobDataMap().put(key,value); 
    //任务运行的时间,SimpleSchedle类型触发器有效
     long time=  System.currentTimeMillis() + 3*1000L; //3秒后启动任务
     Date startTime = new Date(time);//将时间戳转化为标准时间 //4.创建Trigger
     //使用SimpleScheduleBuilder或者CronScheduleBuilder
     Trigger t = TriggerBuilder.newTrigger().withDescription("").withIdentity("ramTrigger", "ramTriggerGroup")
             .startAt(startTime).withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
     //5.注册任务和定时器
     scheduler.scheduleJob(jd, t);
      //6.启动 调度器
     scheduler.start();
    }
}
 

-------------------------------------------------------------------------------------------------------------------------

package com.shp.test.watchService;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import com.shp.util.LoadConfigUtil;
 
public class CronJob implements Job{
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        String value=context.getJobDetail().getJobDataMap().getString("fileName");

        //获取配置文件所有的值
        String values=LoadConfigUtil.getAllValues(value);

        //获取配置文件对应Key的值

        String values=LoadConfigUtil.get(key);
        System.out.println("values="+values);
    }
 
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

测试

package com.shp.test.watchService;
import org.quartz.SchedulerException;
import com.shp.util.LoadConfigUtil;
import com.shp.util.RAMQuartzUtil;
 
public class Demo3 {
    private static String key="fileName";
    private static String value="E:\\aa\\config.ini";
    public static void main(String[] args) throws InterruptedException, SchedulerException {
        LoadConfigUtil.load(value);
        RAMQuartzUtil.startTask(key, value);
    }
 
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值