WatchServer新增通过Path获取WatchKey方法

This commit is contained in:
Looly 2023-12-29 00:19:48 +08:00
parent 1ceb0e5df6
commit c68b9df0df
2 changed files with 21 additions and 18 deletions

View File

@ -2,9 +2,11 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.25(2023-12-28) # 5.8.25(2023-12-29)
### 🐣新特性 ### 🐣新特性
* 【core 】 WatchServer新增通过Path获取WatchKey方法pr#1145@Gitee
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复StrJoin当append内容后调用length()会出现空指针问题issue#3444@Github * 【core 】 修复StrJoin当append内容后调用length()会出现空指针问题issue#3444@Github
* 【core 】 修复PostgreSQL、H2使用upsert字段大小写问题issue#I8PB4X@Gitee * 【core 】 修复PostgreSQL、H2使用upsert字段大小写问题issue#I8PB4X@Gitee

View File

@ -1,17 +1,17 @@
package cn.hutool.core.io; 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.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.Ignore;
import org.junit.Test; import org.junit.Test;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
/** /**
* 文件监听单元测试 * 文件监听单元测试
* *
@ -21,36 +21,37 @@ public class WatchMonitorTest {
WatchMonitor monitor; WatchMonitor monitor;
Watcher watcher = new SimpleWatcher() { Watcher watcher = new SimpleWatcher() {
@Override @Override
public void onCreate(WatchEvent<?> event, Path currentPath) { public void onCreate(final WatchEvent<?> event, final Path currentPath) {
Object obj = event.context(); final Object obj = event.context();
Console.log("创建:{}-> {}", currentPath, obj); Console.log("创建:{}-> {}", currentPath, obj);
WatchKey watchKey = monitor.getWatchKey(currentPath); final WatchKey watchKey = monitor.getWatchKey(currentPath);
Path watchable = (Path) watchKey.watchable(); final Path watchable = (Path) watchKey.watchable();
Path fullPath = watchable.resolve((Path) event.context()); final Path fullPath = watchable.resolve((Path) event.context());
Console.log("Path 完整对象:{}", fullPath); Console.log("Path 完整对象:{}", fullPath);
} }
@Override @Override
public void onModify(WatchEvent<?> event, Path currentPath) { public void onModify(final WatchEvent<?> event, final Path currentPath) {
Object obj = event.context(); final Object obj = event.context();
Console.log("修改:{}-> {}", currentPath, obj); Console.log("修改:{}-> {}", currentPath, obj);
} }
@Override @Override
public void onDelete(WatchEvent<?> event, Path currentPath) { public void onDelete(final WatchEvent<?> event, final Path currentPath) {
Object obj = event.context(); final Object obj = event.context();
Console.log("删除:{}-> {}", currentPath, obj); Console.log("删除:{}-> {}", currentPath, obj);
} }
@Override @Override
public void onOverflow(WatchEvent<?> event, Path currentPath) { public void onOverflow(final WatchEvent<?> event, final Path currentPath) {
Object obj = event.context(); final Object obj = event.context();
Console.log("Overflow{}-> {}", currentPath, obj); Console.log("Overflow{}-> {}", currentPath, obj);
} }
}; };
@Test @Test
@Ignore
public void testFile() { public void testFile() {
monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500)); monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500));
@ -60,9 +61,9 @@ public class WatchMonitorTest {
} }
@Test @Test
@Ignore
public void testDir() { public void testDir() {
monitor = WatchMonitor.createAll("d:/", new DelayWatcher(watcher, 500)); monitor = WatchMonitor.createAll("d:/", new DelayWatcher(watcher, 500));
monitor.run(); monitor.run();
} }