From 95f37beb6669ff4c233b1b99f27dbc02423d91d2 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Thu, 28 Dec 2023 22:29:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9B=91=E5=90=AC=20WatchServer=20=E6=96=B0=E5=A2=9E=E9=80=9A?= =?UTF-8?q?=E8=BF=87=20Path=20=E8=8E=B7=E5=8F=96=20WatchKey=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/io/watch/WatchServer.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java index f76bdadbc..e9477cf63 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/watch/WatchServer.java @@ -3,6 +3,7 @@ package cn.hutool.core.io.watch; import cn.hutool.core.io.IoUtil; import cn.hutool.core.lang.Filter; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjectUtil; import java.io.Closeable; import java.io.IOException; @@ -128,7 +129,7 @@ public class WatchServer extends Thread implements Closeable, Serializable { /** * 执行事件获取并处理 * - * @param action 监听回调函数,实现此函数接口用于处理WatchEvent事件 + * @param action 监听回调函数,实现此函数接口用于处理WatchEvent事件 * @param watchFilter 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤 * @since 5.4.0 */ @@ -163,7 +164,7 @@ public class WatchServer extends Thread implements Closeable, Serializable { * @param watchFilter 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤 */ public void watch(Watcher watcher, Filter> watchFilter) { - watch((event, currentPath)->{ + watch((event, currentPath) -> { final WatchEvent.Kind kind = event.kind(); if (kind == WatchKind.CREATE.getValue()) { @@ -178,6 +179,21 @@ public class WatchServer extends Thread implements Closeable, Serializable { }, watchFilter); } + /** + * 通过 path 获取 watchKey + * + * @param path path + * @return 如果不存在则返回 null + */ + public WatchKey getWatchKey(Path path) { + for (Map.Entry entry : watchKeyPathMap.entrySet()) { + if (ObjectUtil.equals(path, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } + /** * 关闭监听 */ From d861be5aac5743080e20e304297e59b2157853ee Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Thu, 28 Dec 2023 22:34:13 +0800 Subject: [PATCH 2/2] add WatchMonitorTest --- .../cn/hutool/core/io/WatchMonitorTest.java | 75 +++++++++++-------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/hutool-core/src/test/java/cn/hutool/core/io/WatchMonitorTest.java b/hutool-core/src/test/java/cn/hutool/core/io/WatchMonitorTest.java index ae10dbf98..7769088ff 100755 --- a/hutool-core/src/test/java/cn/hutool/core/io/WatchMonitorTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/WatchMonitorTest.java @@ -2,53 +2,68 @@ package cn.hutool.core.io; import java.nio.file.Path; import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.Watchable; import cn.hutool.core.io.watch.SimpleWatcher; import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.io.watch.Watcher; import cn.hutool.core.io.watch.watchers.DelayWatcher; import cn.hutool.core.lang.Console; +import org.junit.Test; /** * 文件监听单元测试 - * - * @author Looly * + * @author Looly */ public class WatchMonitorTest { + WatchMonitor monitor; + Watcher watcher = new SimpleWatcher() { + @Override + public void onCreate(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("创建:{}-> {}", currentPath, obj); + WatchKey watchKey = monitor.getWatchKey(currentPath); + Path watchable = (Path) watchKey.watchable(); + Path fullPath = watchable.resolve((Path) event.context()); + Console.log("Path 完整对象:{}", fullPath); + } - public static void main(String[] args) { - Watcher watcher = new SimpleWatcher(){ - @Override - public void onCreate(WatchEvent event, Path currentPath) { - Object obj = event.context(); - Console.log("创建:{}-> {}", currentPath, obj); - } + @Override + public void onModify(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("修改:{}-> {}", currentPath, obj); + } - @Override - public void onModify(WatchEvent event, Path currentPath) { - Object obj = event.context(); - Console.log("修改:{}-> {}", currentPath, obj); - } + @Override + public void onDelete(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("删除:{}-> {}", currentPath, obj); + } - @Override - public void onDelete(WatchEvent event, Path currentPath) { - Object obj = event.context(); - Console.log("删除:{}-> {}", currentPath, obj); - } + @Override + public void onOverflow(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Overflow:{}-> {}", currentPath, obj); + } + }; + + + @Test + public void testFile() { + + monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500)); - @Override - public void onOverflow(WatchEvent event, Path currentPath) { - Object obj = event.context(); - Console.log("Overflow:{}-> {}", currentPath, obj); - } - }; - - WatchMonitor monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500)); - monitor.setMaxDepth(0); monitor.start(); } - - + + @Test + public void testDir() { + monitor = WatchMonitor.createAll("d:/", new DelayWatcher(watcher, 500)); + + monitor.run(); + } + }