From c537b0b0bf33b05399f24acd6fc9a5680aa7c2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=8B=87?= Date: Mon, 23 Oct 2023 14:17:47 +0800 Subject: [PATCH] If the monitored folder does not exist, create it. (#748) Co-authored-by: wtt <30461027+wtt40122@users.noreply.github.com> --- .../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 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();