直接贴出来代码:
1 package cn.leadeon.utils.file; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.util.Properties; 8 9 /** 10 * 轮询监控配置文件是否更新,动态获取最新内容 11 * 12 * @author LiJunJun 13 * @date 2018.08.08 14 */ 15 public class MonitorSysPropertiesUtil 16 { 17 private static Properties SysLocalPropObject = null; 18 19 // 配置文件绝对路径 20 private static String defaultPropFilePath = null; 21 22 // 配置文件名称 23 private static String propertiesName = null; 24 25 // 文件更新标识 26 protected long lastModifiedData = -1; 27 28 private static MonitorSysPropertiesUtil instance; 29 30 public static MonitorSysPropertiesUtil getInstance(String fileName) 31 { 32 if (propertiesName == null) 33 { 34 propertiesName = fileName; 35 } 36 37 if (instance == null) 38 { 39 syncInit(); 40 } 41 42 return instance; 43 } 44 45 /** 46 * 同步块,避免多线程调用,生成多个实例 47 */ 48 private static synchronized void syncInit() 49 { 50 if (instance == null) 51 { 52 instance = new MonitorSysPropertiesUtil(); 53 } 54 } 55 56 /** 57 * 私有构造器启动一个线程实时监控配置文件 58 */ 59 private MonitorSysPropertiesUtil() 60 { 61 SysLocalPropObject = new Properties(); 62 63 ClassLoader classLoader = MonitorSysPropertiesUtil.class.getClassLoader(); 64 65 defaultPropFilePath = classLoader.getResource(propertiesName).getFile(); 66 67 File propertyFile = new File(defaultPropFilePath); 68 69 if (propertyFile.exists()) 70 { 71 reloadFile(propertyFile); 72 } 73 74 // 循环监控配置文件的变化,一旦发现文件发生变化了则重读配置文件 75 Thread thread = new Thread(new monitorRunnable(defaultPropFilePath)); 76 77 thread.start(); 78 } 79 80 /** 81 * 监控线程内部类 82 * 83 * @author LiJunJun 84 */ 85 class monitorRunnable implements Runnable 86 { 87 88 private String filePath; 89 90 final MonitorSysPropertiesUtil self = MonitorSysPropertiesUtil.this; 91 92 public monitorRunnable(String filePath) 93 { 94 this.filePath = filePath; 95 } 96 97 @Override 98 public void run() 99 { 100 while (true) 101 { 102 // 间隔10分钟 103 try 104 { 105 Thread.sleep(1000 * 60 * 10); 106 // Thread.sleep(1000); 107 } 108 catch (InterruptedException e) 109 { 110 System.out.println("定时巡检配置文件线程休眠异常" + Thread.currentThread().getName()); 111 e.printStackTrace(); 112 } 113 114 try 115 { 116 File propertyFile = new File(filePath); 117 118 if (self.lastModifiedData != propertyFile.lastModified()) 119 { 120 self.reloadFile(propertyFile); 121 self.lastModifiedData = propertyFile.lastModified(); 122 } 123 } 124 catch (Exception e) 125 { 126 System.out.println("校验配置文件是否改变异常,MonitorSysPropertiesUtil -- monitorRunnable"); 127 e.printStackTrace(); 128 } 129 } 130 } 131 132 } 133 134 /** 135 * 重新加载文件 136 * 137 * @author LiJunJun 2017.08.08 138 * @param propertyFile 配置文件路径 139 */ 140 private void reloadFile(File propertyFile) 141 { 142 FileInputStream inputStreamLocal = null; 143 try 144 { 145 inputStreamLocal = new FileInputStream(propertyFile); 146 147 SysLocalPropObject.load(inputStreamLocal); 148 } 149 catch (Exception e) 150 { 151 if (e instanceof FileNotFoundException) 152 { 153 SysLocalPropObject = null; 154 } 155 else 156 { 157 System.out.println("校验配置文件是否改变异常,MonitorSysPropertiesUtil -- reloadFile failed"); 158 } 159 e.printStackTrace(); 160 } 161 finally 162 { 163 try 164 { 165 if (inputStreamLocal != null) 166 { 167 inputStreamLocal.close(); 168 } 169 } 170 catch (IOException e) 171 { 172 System.out.println("关闭IO流异常,MonitorSysPropertiesUtil -- reloadFile failed"); 173 e.printStackTrace(); 174 } 175 } 176 } 177 178 /** 179 * 对外提供获取配置文件对象的方法 180 */ 181 public static Properties getPropertiesObject() 182 { 183 return SysLocalPropObject; 184 } 185 }