From b06470661100defdf13ea7c2c1a1b76d7258328c Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Mon, 23 Oct 2023 13:50:14 +0800 Subject: [PATCH] If the monitored folder does not exist, create it. --- .../xiaomi/mone/file/ozhera/HeraFileMonitor.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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();