监听文件修改,自动加载xml文件。
生活随笔
收集整理的這篇文章主要介紹了
监听文件修改,自动加载xml文件。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載文章,轉載自,公司項目,董亞杰寫的。
下面是完整的代碼,復制的。
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;@Overridepublic 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() {@Overridepublic 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后。
總結
以上是生活随笔為你收集整理的监听文件修改,自动加载xml文件。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server 2008, 200
- 下一篇: spring 定时任务执行两次解决办法