通过文件的当前时间和上次修改时间做比较,判断是否需要重新读取文件,从而实现不重启服务器获取修改后的参数
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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
用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);
}
}