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 4cbd8ca6a..fdecf1fb4 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 @@ -38,14 +38,17 @@ public class HeraFileMonitor { @Setter private EventListener listener; - public HeraFileMonitor() { + this(TimeUnit.SECONDS.toMillis(30)); + } + + public HeraFileMonitor(long removeTime) { Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { try { List> remList = Lists.newArrayList(); long now = System.currentTimeMillis(); fileMap.values().forEach(it -> { - if (now - it.getUtime().get() >= TimeUnit.SECONDS.toMillis(5)) { + if (now - it.getUtime().get() >= removeTime) { remList.add(Pair.of(it.getFileName(), it.getFileKey())); } }); @@ -60,6 +63,7 @@ public HeraFileMonitor() { }, 5, 10, TimeUnit.SECONDS); } + public HeraFileMonitor(EventListener listener) { this(); this.listener = listener; @@ -69,6 +73,11 @@ public void reg(String path) throws IOException, InterruptedException { Path directory = Paths.get(path); File f = directory.toFile(); + if (!f.exists()) { + log.info("create directory:{}", directory); + Files.createDirectories(directory); + } + Arrays.stream(f.listFiles()).forEach(it -> initFile(it)); WatchService watchService = FileSystems.getDefault().newWatchService();