mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
fix watchService
This commit is contained in:
parent
52fb6267a5
commit
5d99de9b89
@ -1,30 +1,5 @@
|
||||
package cn.hutool.core.io.watch;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.nio.file.ClosedWatchServiceException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
@ -33,6 +8,18 @@ import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.*;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 路径监听器
|
||||
*
|
||||
@ -97,6 +84,10 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
||||
* 监听事件列表
|
||||
*/
|
||||
private WatchEvent.Kind<?>[] events;
|
||||
/**
|
||||
* 监听选项,例如监听频率等
|
||||
*/
|
||||
private WatchEvent.Modifier[] modifiers;
|
||||
|
||||
/**
|
||||
* 监听是否已经关闭
|
||||
@ -450,6 +441,22 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置监听选项,例如监听频率等,可设置项包括:<p>
|
||||
*
|
||||
* <pre>
|
||||
* 1、com.sun.nio.file.StandardWatchEventKinds
|
||||
* 2、com.sun.nio.file.SensitivityWatchEventModifier
|
||||
* </pre>
|
||||
*
|
||||
* @param modifiers 监听选项,例如监听频率等
|
||||
* @return this
|
||||
*/
|
||||
public WatchMonitor setModifiers(WatchEvent.Modifier[] modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭监听
|
||||
*/
|
||||
@ -466,7 +473,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
||||
*
|
||||
* @param watcher {@link Watcher}
|
||||
*/
|
||||
private void doTakeAndWatch(Watcher watcher){
|
||||
private void doTakeAndWatch(Watcher watcher) {
|
||||
WatchKey wk;
|
||||
try {
|
||||
wk = watchService.take();
|
||||
@ -513,9 +520,18 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
|
||||
* @param maxDepth 递归下层目录的最大深度
|
||||
*/
|
||||
private void registerPath(Path path, int maxDepth) {
|
||||
final WatchEvent.Kind<?>[] kinds = ArrayUtil.defaultIfEmpty(this.events, EVENTS_ALL);
|
||||
|
||||
try {
|
||||
final WatchKey key = path.register(this.watchService, ArrayUtil.defaultIfEmpty(this.events, EVENTS_ALL));
|
||||
final WatchKey key;
|
||||
if (ArrayUtil.isEmpty(this.modifiers)) {
|
||||
key = path.register(this.watchService, kinds);
|
||||
} else {
|
||||
key = path.register(this.watchService, kinds, this.modifiers);
|
||||
}
|
||||
watchKeyPathMap.put(key, path);
|
||||
|
||||
// 递归注册下一层层级的目录
|
||||
if (maxDepth > 1) {
|
||||
//遍历所有子目录并加入监听
|
||||
Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), maxDepth, new SimpleFileVisitor<Path>() {
|
||||
|
@ -1,12 +1,12 @@
|
||||
package cn.hutool.core.io.watch;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.*;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
|
||||
/**
|
||||
@ -377,4 +377,21 @@ public class WatchUtil {
|
||||
watchMonitor.setWatcher(watcher);
|
||||
return watchMonitor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册Watchable对象到WatchService服务
|
||||
*
|
||||
* @param watchable 可注册对象
|
||||
* @param watcher WatchService对象
|
||||
* @param events 监听事件
|
||||
* @return {@link WatchKey}
|
||||
* @since 4.6.9
|
||||
*/
|
||||
public static WatchKey register(Watchable watchable, WatchService watcher, WatchEvent.Kind<?>... events){
|
||||
try {
|
||||
return watchable.register(watcher, events);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user