!1145 新增 文件监听 WatchServer 新增通过 Path 获取 WatchKey 方法

Merge pull request !1145 from 蒋小小/v5-dev
This commit is contained in:
Looly 2023-12-28 16:14:20 +00:00 committed by Gitee
commit 1ceb0e5df6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 63 additions and 32 deletions

View File

@ -3,6 +3,7 @@ package cn.hutool.core.io.watch;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Filter; import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
@ -178,6 +179,21 @@ public class WatchServer extends Thread implements Closeable, Serializable {
}, watchFilter); }, watchFilter);
} }
/**
* 通过 path 获取 watchKey
*
* @param path path
* @return 如果不存在则返回 null
*/
public WatchKey getWatchKey(Path path) {
for (Map.Entry<WatchKey, Path> entry : watchKeyPathMap.entrySet()) {
if (ObjectUtil.equals(path, entry.getValue())) {
return entry.getKey();
}
}
return null;
}
/** /**
* 关闭监听 * 关闭监听
*/ */

View File

@ -2,27 +2,32 @@ package cn.hutool.core.io;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.WatchEvent; 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.SimpleWatcher;
import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher; import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.io.watch.watchers.DelayWatcher; import cn.hutool.core.io.watch.watchers.DelayWatcher;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import org.junit.Test;
/** /**
* 文件监听单元测试 * 文件监听单元测试
* *
* @author Looly * @author Looly
*
*/ */
public class WatchMonitorTest { public class WatchMonitorTest {
WatchMonitor monitor;
public static void main(String[] args) {
Watcher watcher = new SimpleWatcher() { Watcher watcher = new SimpleWatcher() {
@Override @Override
public void onCreate(WatchEvent<?> event, Path currentPath) { public void onCreate(WatchEvent<?> event, Path currentPath) {
Object obj = event.context(); Object obj = event.context();
Console.log("创建:{}-> {}", currentPath, obj); 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);
} }
@Override @Override
@ -44,11 +49,21 @@ public class WatchMonitorTest {
} }
}; };
WatchMonitor monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500));
@Test
public void testFile() {
monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500));
monitor.setMaxDepth(0); monitor.setMaxDepth(0);
monitor.start(); monitor.start();
} }
@Test
public void testDir() {
monitor = WatchMonitor.createAll("d:/", new DelayWatcher(watcher, 500));
monitor.run();
}
} }