mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add WatchMonitorTest
This commit is contained in:
parent
95f37beb66
commit
d861be5aac
@ -2,53 +2,68 @@ 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;
|
||||||
|
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) {
|
@Override
|
||||||
Watcher watcher = new SimpleWatcher(){
|
public void onModify(WatchEvent<?> event, Path currentPath) {
|
||||||
@Override
|
Object obj = event.context();
|
||||||
public void onCreate(WatchEvent<?> event, Path currentPath) {
|
Console.log("修改:{}-> {}", currentPath, obj);
|
||||||
Object obj = event.context();
|
}
|
||||||
Console.log("创建:{}-> {}", currentPath, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onModify(WatchEvent<?> event, Path currentPath) {
|
public void onDelete(WatchEvent<?> event, Path currentPath) {
|
||||||
Object obj = event.context();
|
Object obj = event.context();
|
||||||
Console.log("修改:{}-> {}", currentPath, obj);
|
Console.log("删除:{}-> {}", currentPath, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDelete(WatchEvent<?> event, Path currentPath) {
|
public void onOverflow(WatchEvent<?> event, Path currentPath) {
|
||||||
Object obj = event.context();
|
Object obj = event.context();
|
||||||
Console.log("删除:{}-> {}", currentPath, obj);
|
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.setMaxDepth(0);
|
||||||
monitor.start();
|
monitor.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDir() {
|
||||||
|
monitor = WatchMonitor.createAll("d:/", new DelayWatcher(watcher, 500));
|
||||||
|
|
||||||
|
monitor.run();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user