文件热加载

package cn.newtouch.test.hotload;


import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class HotLoadConf implements Runnable{
private WatchService watcher;
// 监控目录
public static String path = "D:/workspace/TestTimer/resource";
private HashSet<String> RefChangeSet = null;
private static HotLoadConf classWatcherService = null;
private Object ClassTemp;
private boolean IsRuner = false;
private static Properties prop=null;
private static HashMap<String,HashMap<ArrayList<String>, String>> datamap=null;
private HotLoadConf() {
try {

watcher = FileSystems.getDefault().newWatchService();
System.out.println(path);
Paths.get(path).register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
RefChangeSet = new HashSet<String>(20);
} catch (IOException e) {
e.printStackTrace();
}
}


public static HotLoadConf GetInstance(String r) throws IOException, SAXException, ParserConfigurationException {
// prop=getProp(r);
datamap=getDatamap(r);
if (classWatcherService == null)
classWatcherService = new HotLoadConf();
classWatcherService.RefChangeSet.add(r);
return classWatcherService;
}
public boolean IsRun() {
return IsRuner;
}


public static Properties getProp(String r) throws IOException{
ClassLoader classLoader = HotLoadConf.class.getClassLoader();
if (prop==null){
prop = new Properties();
InputStream inStream =null; 
inStream=classLoader.getResourceAsStream(r);
prop.load(inStream);
}


return prop;

}


 
public HotLoadConf StartServers() {
if (!IsRuner)
new Thread(this).start();
return this;
}



/**
* 监控文件
*/
private void handleEvents() {
while (true) {
try {
WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
continue;
}
@SuppressWarnings("unchecked")
WatchEvent<Path> e = (WatchEvent<Path>) event;
Path fileName = e.context();
// URL resource = classLoader.getResource(r);
// String str=resource+"";
// System.out.println(str);
// String[] split = str.split(":/");
// String str1=split[1]+":/"+split[2];
if(RefChangeSet.contains(fileName+"")){
datamap=LoadDatamap(fileName+"") ;
System.err.println("发现目录下有文件发生变化.进行热加载" + path + "/" + fileName);
// File filepath = new File(path + "/" + fileName);
// FileInputStream inStream = new FileInputStream(filepath);
// prop.load(inStream);
// String property = prop.getProperty("registry.address");
// System.out.println(property);
}
}
if (!key.reset()) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
if (!IsRuner) {
IsRuner = !IsRuner;
handleEvents();
}
System.err.println("文件监听已运行");
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值