This commit is contained in:
Looly 2023-12-29 12:38:14 +08:00
parent bca0f82bac
commit 03b8f57648
6 changed files with 46 additions and 48 deletions

View File

@ -19,7 +19,6 @@ import org.dromara.hutool.core.text.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
@ -47,7 +46,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
/**
* 监听的文件对于单文件监听不为空
*/
private Path filePath;
private Path file;
/**
* 递归目录的最大深度当小于1时不递归下层目录
@ -61,7 +60,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
/**
* 构造
*
* @param dir 字符串路径
* @param dir 字符串路径
* @param events 监听事件列表
*/
public WatchMonitor(final Path dir, final WatchEvent.Kind<?>... events) {
@ -77,7 +76,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
* maxDepth = 3 表示监听当前目录以及下两层
* </pre>
*
* @param dir 字符串路径
* @param dir 路径
* @param maxDepth 递归目录的最大深度当小于2时不递归下层目录
* @param events 监听事件列表
*/
@ -150,6 +149,11 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
return this;
}
@Override
public void close() {
this.watchService.close();
}
//------------------------------------------------------ private method start
/**
@ -171,8 +175,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
final String lastPathEleStr = lastPathEle.toString();
//带有点表示有扩展名按照未创建的文件对待Linux下.d的为目录排除之
if (StrUtil.contains(lastPathEleStr, CharUtil.DOT) && !StrUtil.endWithIgnoreCase(lastPathEleStr, ".d")) {
this.filePath = this.dir;
this.dir = this.filePath.getParent();
this.file = this.dir;
this.dir = this.file.getParent();
}
}
@ -180,8 +184,8 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
PathUtil.mkdir(this.dir);
} else if (PathUtil.isFile(this.dir, false)) {
// 文件路径
this.filePath = this.dir;
this.dir = this.filePath.getParent();
this.file = this.dir;
this.dir = this.file.getParent();
}
}
@ -191,19 +195,14 @@ public class WatchMonitor extends Thread implements Closeable, Serializable {
* @param watcher {@link Watcher}
*/
private void doTakeAndWatch(final Watcher watcher) {
this.watchService.watch(watcher, watchEvent -> null == filePath || filePath.endsWith(watchEvent.context().toString()));
this.watchService.watch(watcher, watchEvent -> null == file || file.endsWith(watchEvent.context().toString()));
}
/**
* 注册监听路径
*/
private void registerPath() {
this.watchService.registerPath(this.dir, (null != this.filePath) ? 0 : this.maxDepth);
}
@Override
public void close() {
this.watchService.close();
this.watchService.registerPath(this.dir, (null != this.file) ? 0 : this.maxDepth);
}
//------------------------------------------------------ private method end
}

View File

@ -264,14 +264,13 @@ public class WatchUtil {
* @param watcher {@link Watcher}
* @return {@link WatchMonitor}
*/
@SuppressWarnings("resource")
public static WatchMonitor ofAll(final Path path, final int maxDepth, final Watcher watcher) {
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.ALL);
watchMonitor.setWatcher(watcher);
return watchMonitor;
return of(path, maxDepth, WatchKind.ALL).setWatcher(watcher);
}
// endregion
// region ----- createModify
// region ----- ofModify
/**
* 创建并初始化监听监听修改事件
*
@ -280,8 +279,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final URL url, final Watcher watcher) {
return createModify(url, 0, watcher);
public static WatchMonitor ofModify(final URL url, final Watcher watcher) {
return ofModify(url, 0, watcher);
}
/**
@ -293,8 +292,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final URL url, final int maxDepth, final Watcher watcher) {
return createModify(UrlUtil.toURI(url), maxDepth, watcher);
public static WatchMonitor ofModify(final URL url, final int maxDepth, final Watcher watcher) {
return ofModify(UrlUtil.toURI(url), maxDepth, watcher);
}
/**
@ -305,8 +304,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final URI uri, final Watcher watcher) {
return createModify(uri, 0, watcher);
public static WatchMonitor ofModify(final URI uri, final Watcher watcher) {
return ofModify(uri, 0, watcher);
}
/**
@ -318,8 +317,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final URI uri, final int maxDepth, final Watcher watcher) {
return createModify(Paths.get(uri), maxDepth, watcher);
public static WatchMonitor ofModify(final URI uri, final int maxDepth, final Watcher watcher) {
return ofModify(Paths.get(uri), maxDepth, watcher);
}
/**
@ -330,8 +329,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final File file, final Watcher watcher) {
return createModify(file, 0, watcher);
public static WatchMonitor ofModify(final File file, final Watcher watcher) {
return ofModify(file, 0, watcher);
}
/**
@ -343,8 +342,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final File file, final int maxDepth, final Watcher watcher) {
return createModify(file.toPath(), maxDepth, watcher);
public static WatchMonitor ofModify(final File file, final int maxDepth, final Watcher watcher) {
return ofModify(file.toPath(), maxDepth, watcher);
}
/**
@ -355,8 +354,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final String path, final Watcher watcher) {
return createModify(path, 0, watcher);
public static WatchMonitor ofModify(final String path, final Watcher watcher) {
return ofModify(path, 0, watcher);
}
/**
@ -368,8 +367,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final String path, final int maxDepth, final Watcher watcher) {
return createModify(Paths.get(path), maxDepth, watcher);
public static WatchMonitor ofModify(final String path, final int maxDepth, final Watcher watcher) {
return ofModify(Paths.get(path), maxDepth, watcher);
}
/**
@ -380,8 +379,8 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final Path path, final Watcher watcher) {
return createModify(path, 0, watcher);
public static WatchMonitor ofModify(final Path path, final Watcher watcher) {
return ofModify(path, 0, watcher);
}
/**
@ -393,7 +392,7 @@ public class WatchUtil {
* @return {@link WatchMonitor}
* @since 4.5.2
*/
public static WatchMonitor createModify(final Path path, final int maxDepth, final Watcher watcher) {
public static WatchMonitor ofModify(final Path path, final int maxDepth, final Watcher watcher) {
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.MODIFY.getValue());
watchMonitor.setWatcher(watcher);
return watchMonitor;

View File

@ -25,7 +25,7 @@ public interface Watcher {
* 文件创建时执行的方法
*
* @param event 事件
* @param key 事件发生的{@link WatchKey}
* @param key 事件发生的{@link WatchKey}可以通过{@link WatchKey#watchable()}获取监听的Path路径
*/
void onCreate(WatchEvent<?> event, WatchKey key);
@ -34,7 +34,7 @@ public interface Watcher {
* 文件修改可能触发多次
*
* @param event 事件
* @param key 事件发生的{@link WatchKey}
* @param key 事件发生的{@link WatchKey}可以通过{@link WatchKey#watchable()}获取监听的Path路径
*/
void onModify(WatchEvent<?> event, WatchKey key);
@ -42,7 +42,7 @@ public interface Watcher {
* 文件删除时执行的方法
*
* @param event 事件
* @param key 事件发生的{@link WatchKey}
* @param key 事件发生的{@link WatchKey}可以通过{@link WatchKey#watchable()}获取监听的Path路径
*/
void onDelete(WatchEvent<?> event, WatchKey key);
@ -50,7 +50,7 @@ public interface Watcher {
* 事件丢失或出错时执行的方法
*
* @param event 事件
* @param key 事件发生的{@link WatchKey}
* @param key 事件发生的{@link WatchKey}可以通过{@link WatchKey#watchable()}获取监听的Path路径
*/
void onOverflow(WatchEvent<?> event, WatchKey key);
}

View File

@ -19,6 +19,7 @@ import org.dromara.hutool.core.io.watch.watchers.DelayWatcher;
import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher;
import org.dromara.hutool.core.lang.Console;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
@ -35,6 +36,7 @@ public class WatchMonitorTest {
@Override
public void onCreate(final WatchEvent<?> event, final WatchKey key) {
final Object obj = event.context();
Console.log(((Path)obj).toAbsolutePath());
Console.log("创建:{}-> {}", key.watchable(), obj);
}

View File

@ -213,7 +213,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
Assert.notNull(this.resource, "Setting resource must be not null !");
// 先关闭之前的监听
IoUtil.closeQuietly(this.watchMonitor);
this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() {
this.watchMonitor = WatchUtil.ofModify(resource.getUrl(), new SimpleWatcher() {
@Override
public void onModify(final WatchEvent<?> event, final WatchKey key) {
final boolean success = load();

View File

@ -228,11 +228,9 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
public void autoLoad(final boolean autoReload) {
if (autoReload) {
Assert.notNull(this.resource, "Properties resource must be not null!");
if (null != this.watchMonitor) {
// 先关闭之前的监听
this.watchMonitor.close();
}
this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() {
// 先关闭之前的监听
IoUtil.closeQuietly(this.watchMonitor);
this.watchMonitor = WatchUtil.ofModify(this.resource.getUrl(), new SimpleWatcher() {
@Override
public void onModify(final WatchEvent<?> event, final WatchKey key) {
load();