转载文章,转载自,公司项目,董亚杰写的。
下面是完整的代码,复制的。
package cn.digitalpublishing.util.debug; import java.io.File; import java.nio.file.FileSystems; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchEvent.Kind; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import org.apache.commons.io.FilenameUtils; import cn.digitalpublishing.util.SpringUtils; import com.ingenta.framework.dao.impl.support.DefaultIngentaDao; /** * * @author dongyajie * */ @WebServlet(initParams = { @WebInitParam(name = "path", value = "W:\\workspace2\\Editorial\\src\\main\\resources\\sql") }, value = "/DynamicReloadHqlXmlNew", loadOnStartup = 88) public class DynamicReloadHqlXmlNew extends HttpServlet { private static final long serialVersionUID = 1L; private final String ACTION_ENTRY_CREATE = "ENTRY_CREATE"; private final String ACTION_ENTRY_DELETE = "ENTRY_DELETE"; private final String ACTION_ENTRY_MODIFY = "ENTRY_MODIFY"; private String prevFileName = ""; private String prevAction = ""; private long prevTimeMillis = 0; private final long interval = 500; private DefaultIngentaDao dao; @Override public void init(ServletConfig config) throws ServletException { final String hqlDir = config.getInitParameter("path"); dao = (DefaultIngentaDao) SpringUtils.getBean("ingentaDao"); ExecutorService service = Executors.newSingleThreadExecutor(); service.execute(new Runnable() { @Override public void run() { WatchService watchService = null; try { watchService = FileSystems.getDefault().newWatchService(); Paths.get(hqlDir).register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); } catch (Exception e) { } if (watchService != null) { while (true) { try { WatchKey key = watchService.take(); List<WatchEvent<?>> events = key.pollEvents(); if (!events.isEmpty()) { WatchEvent<?> event = events.get(events.size() - 1); File eventFile = new File(hqlDir, event.context().toString()); Kind<?> eventKind = event.kind(); String kindName = eventKind.name(); if (eventKind.name().equals(ACTION_ENTRY_CREATE)) { kindName = ACTION_ENTRY_MODIFY; } if ((!eventFile.getPath().equals(prevFileName) || !kindName.equals(prevAction)) || ((System.currentTimeMillis() - prevTimeMillis) > interval)) { if (eventFile.isFile()) { if (kindName.equals(ACTION_ENTRY_MODIFY)) { if (FilenameUtils.isExtension(eventFile.getName().toLowerCase(), "xml")) { loadXML(eventFile.getPath()); } } } if (kindName.equals(ACTION_ENTRY_DELETE)) { } prevAction = kindName; prevFileName = eventFile.getPath(); prevTimeMillis = System.currentTimeMillis(); } } if (!key.reset()) { break; } } catch (Exception e) { } } } } }); service.shutdown(); } private synchronized void loadXML(String filename) { String logfn = FilenameUtils.getName(filename); try { dao.loadResource(new File(filename)); System.err.println(logfn + " 加载完成"); } catch (Exception e) { System.err.println(logfn + " 加载失败" + e.getMessage()); } } }
反正,暂时我是看不懂,感觉太高端了,总有一天我有看懂的时候,,,,,
大神,工作经验5年,编程经验,10年,90后。
时间: 2024-11-02 18:55:15