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 11787323f..124a52293 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 @@ -40,14 +40,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())); } }); @@ -62,6 +65,7 @@ public HeraFileMonitor() { }, 5, 10, TimeUnit.SECONDS); } + public HeraFileMonitor(EventListener listener) { this(); this.listener = listener; @@ -71,6 +75,11 @@ public void reg(String path, Predicate predicate) throws IOException, In Path directory = Paths.get(path); File f = directory.toFile(); + if (!f.exists()) { + log.info("create directory:{}", directory); + Files.createDirectories(directory); + } + Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(it -> predicate.test(it.getPath())).forEach(this::initFile); WatchService watchService = FileSystems.getDefault().newWatchService();