diff --git a/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java b/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java index aa3a841b2..f7add647a 100644 --- a/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java +++ b/jcommon/file/src/main/java/com/xiaomi/mone/file/ozhera/HeraFileMonitor.java @@ -84,48 +84,57 @@ public void reg(String path, Predicate predicate) throws IOException, In WatchService watchService = FileSystems.getDefault().newWatchService(); directory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); while (true) { - WatchKey key = watchService.take(); - for (WatchEvent event : key.pollEvents()) { - Path modifiedFile = (Path) event.context(); - String filePath = String.format("%s%s", path, modifiedFile.getFileName().toString()); - if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) { - continue; - } - HeraFile hfile = fileMap.get(filePath); - - if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { - if (null == hfile) { - hfile = initFile(new File(filePath)); - } - modify(hfile); - } - - if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { - fileMap.remove(filePath); - if (null != hfile) { - map.remove(hfile.getFileKey()); - listener.onEvent(FileEvent.builder().type(EventType.delete).fileName(filePath).fileKey(hfile.getFileKey()).build()); - } - } - - if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { - File file = new File(filePath); - Object k = FileUtils.fileKey(file); - - if (map.containsKey(k)) { - log.info("change name " + map.get(k) + "--->" + file); - listener.onEvent(FileEvent.builder().fileKey(k).type(EventType.rename).build()); - } else { - log.info("ENTRY_CREATE filePath:{},fileKey:{}", filePath, k); - HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build(); - map.putIfAbsent(k, hf); - fileMap.put(filePath, hf); - - listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build()); + try { + WatchKey key = watchService.take(); + try { + for (WatchEvent event : key.pollEvents()) { + Path modifiedFile = (Path) event.context(); + String filePath = String.format("%s%s", path, modifiedFile.getFileName().toString()); + if (!predicate.test(filePath) || modifiedFile.getFileName().toString().startsWith(".")) { + continue; + } + HeraFile hfile = fileMap.get(filePath); + + if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { + if (null == hfile) { + hfile = initFile(new File(filePath)); + } + modify(hfile); + } + + if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { + fileMap.remove(filePath); + if (null != hfile) { + map.remove(hfile.getFileKey()); + listener.onEvent(FileEvent.builder().type(EventType.delete).fileName(filePath).fileKey(hfile.getFileKey()).build()); + } + } + + if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { + File file = new File(filePath); + Object k = FileUtils.fileKey(file); + + if (map.containsKey(k)) { + log.info("change name " + map.get(k) + "--->" + file); + listener.onEvent(FileEvent.builder().fileKey(k).type(EventType.rename).build()); + } else { + log.info("ENTRY_CREATE filePath:{},fileKey:{}", filePath, k); + HeraFile hf = HeraFile.builder().file(file).fileKey(k).fileName(filePath).build(); + map.putIfAbsent(k, hf); + fileMap.put(filePath, hf); + + listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build()); + } + } } + } catch (Exception e1) { + log.error("watchService poll events error", e1); + } finally { + key.reset(); } + } catch (Exception e) { + log.error("watchService error", e); } - key.reset(); } } @@ -165,11 +174,13 @@ private HeraFile initFile(File it) { private void modify(HeraFile hfile) { - hfile.getUtime().set(System.currentTimeMillis()); - if (hfile.getFile().length() == 0) { - listener.onEvent(FileEvent.builder().type(EventType.empty).fileName(hfile.getFileName()).fileKey(hfile.getFileKey()).build()); - } else { - listener.onEvent(FileEvent.builder().type(EventType.modify).build()); + if (null != hfile) { + hfile.getUtime().set(System.currentTimeMillis()); + if (hfile.getFile().length() == 0) { + listener.onEvent(FileEvent.builder().type(EventType.empty).fileName(hfile.getFileName()).fileKey(hfile.getFileKey()).build()); + } else { + listener.onEvent(FileEvent.builder().type(EventType.modify).build()); + } } } diff --git a/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java b/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java index b0882d720..363f59624 100644 --- a/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java +++ b/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java @@ -87,7 +87,7 @@ public void testLogFileMonitor() { @Test public void testLogWS() throws IOException { - LogFileWS log = new LogFileWS("D:\\t", new ReadListener() { + LogFile log = new LogFile("D:\\test.log", new ReadListener() { @Override public void onEvent(ReadEvent event) { System.out.println(event.getReadResult().getLines()); @@ -132,4 +132,5 @@ public void testReadFileCutting() throws IOException { log.readLine(); System.in.read(); } + }