From bca0f82bac78523981f147f39191397b0acc6993 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 29 Dec 2023 11:50:07 +0800 Subject: [PATCH] fix code --- .../hutool/core/io/file/LineReadWatcher.java | 3 +- .../dromara/hutool/core/io/file/Tailer.java | 16 +++----- .../hutool/core/io/watch/WatchMonitor.java | 2 +- .../core/io/watch/watchers/DelayWatcher.java | 37 +++++++++---------- .../core/io/watch/watchers/IgnoreWatcher.java | 14 +++---- .../core/io/watch/watchers/SimpleWatcher.java | 3 -- .../core/io/watch/watchers/WatcherChain.java | 18 ++++----- .../hutool/core/io/WatchMonitorTest.java | 24 ++++++------ .../org/dromara/hutool/setting/Setting.java | 36 +++++++----------- .../dromara/hutool/setting/props/Props.java | 6 +-- 10 files changed, 71 insertions(+), 88 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/LineReadWatcher.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/LineReadWatcher.java index 441ad8517..4d2d73f67 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/LineReadWatcher.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/LineReadWatcher.java @@ -21,6 +21,7 @@ import java.io.RandomAccessFile; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; /** * 行处理的Watcher实现 @@ -53,7 +54,7 @@ public class LineReadWatcher extends SimpleWatcher implements Runnable { } @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { final RandomAccessFile randomAccessFile = this.randomAccessFile; final Charset charset = this.charset; final SerConsumer lineHandler = this.lineHandler; diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/Tailer.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/Tailer.java index b5f406703..57d252491 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/Tailer.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/Tailer.java @@ -14,14 +14,14 @@ package org.dromara.hutool.core.io.file; import org.dromara.hutool.core.date.DateUnit; import org.dromara.hutool.core.exception.HutoolException; +import org.dromara.hutool.core.func.SerConsumer; import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IoUtil; -import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; import org.dromara.hutool.core.io.watch.WatchKind; import org.dromara.hutool.core.io.watch.WatchMonitor; import org.dromara.hutool.core.io.watch.WatchUtil; +import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; import org.dromara.hutool.core.lang.Console; -import org.dromara.hutool.core.func.SerConsumer; import org.dromara.hutool.core.text.CharUtil; import org.dromara.hutool.core.util.CharsetUtil; @@ -30,14 +30,10 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.io.Serializable; import java.nio.charset.Charset; -import java.nio.file.Path; import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; import java.util.Stack; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; /** * 文件内容跟随器,实现类似Linux下"tail -f"命令功能 @@ -162,8 +158,8 @@ public class Tailer implements Serializable { fileDeleteWatchMonitor = WatchUtil.of(this.filePath, WatchKind.DELETE.getValue()); fileDeleteWatchMonitor.setWatcher(new SimpleWatcher(){ @Override - public void onDelete(final WatchEvent event, final Path currentPath) { - super.onDelete(event, currentPath); + public void onDelete(final WatchEvent event, final WatchKey key) { + super.onDelete(event, key); stop(); throw new IORuntimeException("{} has been deleted", filePath); } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java index d6b6575aa..83ec7c3ee 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/WatchMonitor.java @@ -202,7 +202,7 @@ public class WatchMonitor extends Thread implements Closeable, Serializable { } @Override - public void close() throws IOException { + public void close() { this.watchService.close(); } //------------------------------------------------------ private method end diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/DelayWatcher.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/DelayWatcher.java index b76ee4b49..0d4f70f52 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/DelayWatcher.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/DelayWatcher.java @@ -17,10 +17,7 @@ import org.dromara.hutool.core.io.watch.Watcher; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.thread.ThreadUtil; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.WatchEvent; -import java.nio.file.WatchService; +import java.nio.file.*; import java.util.Set; /** @@ -59,37 +56,37 @@ public class DelayWatcher implements Watcher { //---------------------------------------------------------------------------------------------------------- Constructor end @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { if(this.delay < 1) { - this.watcher.onModify(event, currentPath); + this.watcher.onModify(event, key); }else { - onDelayModify(event, currentPath); + onDelayModify(event, key); } } @Override - public void onCreate(final WatchEvent event, final Path currentPath) { - watcher.onCreate(event, currentPath); + public void onCreate(final WatchEvent event, final WatchKey key) { + watcher.onCreate(event, key); } @Override - public void onDelete(final WatchEvent event, final Path currentPath) { - watcher.onDelete(event, currentPath); + public void onDelete(final WatchEvent event, final WatchKey key) { + watcher.onDelete(event, key); } @Override - public void onOverflow(final WatchEvent event, final Path currentPath) { - watcher.onOverflow(event, currentPath); + public void onOverflow(final WatchEvent event, final WatchKey key) { + watcher.onOverflow(event, key); } //---------------------------------------------------------------------------------------------------------- Private method start /** * 触发延迟修改 * @param event 事件 - * @param currentPath 事件发生的当前Path路径 + * @param key {@link WatchKey} */ - private void onDelayModify(final WatchEvent event, final Path currentPath) { - final Path eventPath = Paths.get(currentPath.toString(), event.context().toString()); + private void onDelayModify(final WatchEvent event, final WatchKey key) { + final Path eventPath = Paths.get(key.watchable().toString(), event.context().toString()); if(eventSet.contains(eventPath)) { //此事件已经被触发过,后续事件忽略,等待统一处理。 return; @@ -97,7 +94,7 @@ public class DelayWatcher implements Watcher { //事件第一次触发,此时标记事件,并启动处理线程延迟处理,处理结束后会删除标记 eventSet.add(eventPath); - startHandleModifyThread(event, currentPath); + startHandleModifyThread(event, key); } /** @@ -106,11 +103,11 @@ public class DelayWatcher implements Watcher { * @param event 事件 * @param currentPath 事件发生的当前Path路径 */ - private void startHandleModifyThread(final WatchEvent event, final Path currentPath) { + private void startHandleModifyThread(final WatchEvent event, final WatchKey key) { ThreadUtil.execute(() -> { ThreadUtil.sleep(delay); - eventSet.remove(Paths.get(currentPath.toString(), event.context().toString())); - watcher.onModify(event, currentPath); + eventSet.remove(Paths.get(key.watchable().toString(), event.context().toString())); + watcher.onModify(event, key); }); } //---------------------------------------------------------------------------------------------------------- Private method end diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/IgnoreWatcher.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/IgnoreWatcher.java index 567f92493..cc890a1bc 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/IgnoreWatcher.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/IgnoreWatcher.java @@ -12,11 +12,11 @@ package org.dromara.hutool.core.io.watch.watchers; -import java.nio.file.Path; -import java.nio.file.WatchEvent; - import org.dromara.hutool.core.io.watch.Watcher; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; + /** * 跳过所有事件处理Watcher
* 用户继承此类后实现需要监听的方法 @@ -27,18 +27,18 @@ import org.dromara.hutool.core.io.watch.Watcher; public class IgnoreWatcher implements Watcher { @Override - public void onCreate(final WatchEvent event, final Path currentPath) { + public void onCreate(final WatchEvent event, final WatchKey key) { } @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { } @Override - public void onDelete(final WatchEvent event, final Path currentPath) { + public void onDelete(final WatchEvent event, final WatchKey key) { } @Override - public void onOverflow(final WatchEvent event, final Path currentPath) { + public void onOverflow(final WatchEvent event, final WatchKey key) { } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/SimpleWatcher.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/SimpleWatcher.java index 7afdd5663..6607e09d2 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/SimpleWatcher.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/SimpleWatcher.java @@ -12,8 +12,6 @@ package org.dromara.hutool.core.io.watch.watchers; -import org.dromara.hutool.core.io.watch.watchers.IgnoreWatcher; - /** * 空白WatchListener
* 用户继承此类后实现需要监听的方法 @@ -21,5 +19,4 @@ import org.dromara.hutool.core.io.watch.watchers.IgnoreWatcher; * */ public class SimpleWatcher extends IgnoreWatcher { - } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/WatcherChain.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/WatcherChain.java index 7ec65214e..abf729b33 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/WatcherChain.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/watch/watchers/WatcherChain.java @@ -15,8 +15,8 @@ package org.dromara.hutool.core.io.watch.watchers; import org.dromara.hutool.core.io.watch.Watcher; import org.dromara.hutool.core.lang.Chain; -import java.nio.file.Path; import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -51,30 +51,30 @@ public class WatcherChain implements Watcher, Chain { } @Override - public void onCreate(final WatchEvent event, final Path currentPath) { + public void onCreate(final WatchEvent event, final WatchKey key) { for (final Watcher watcher : chain) { - watcher.onCreate(event, currentPath); + watcher.onCreate(event, key); } } @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { for (final Watcher watcher : chain) { - watcher.onModify(event, currentPath); + watcher.onModify(event, key); } } @Override - public void onDelete(final WatchEvent event, final Path currentPath) { + public void onDelete(final WatchEvent event, final WatchKey key) { for (final Watcher watcher : chain) { - watcher.onDelete(event, currentPath); + watcher.onDelete(event, key); } } @Override - public void onOverflow(final WatchEvent event, final Path currentPath) { + public void onOverflow(final WatchEvent event, final WatchKey key) { for (final Watcher watcher : chain) { - watcher.onOverflow(event, currentPath); + watcher.onOverflow(event, key); } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java index 26c8d26c7..edb41b3dc 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/io/WatchMonitorTest.java @@ -12,16 +12,16 @@ package org.dromara.hutool.core.io; -import java.nio.file.Path; -import java.nio.file.WatchEvent; - -import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; import org.dromara.hutool.core.io.watch.WatchMonitor; import org.dromara.hutool.core.io.watch.WatchUtil; import org.dromara.hutool.core.io.watch.Watcher; 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.WatchEvent; +import java.nio.file.WatchKey; + /** * 文件监听单元测试 * @@ -33,27 +33,27 @@ public class WatchMonitorTest { public static void main(final String[] args) { final Watcher watcher = new SimpleWatcher(){ @Override - public void onCreate(final WatchEvent event, final Path currentPath) { + public void onCreate(final WatchEvent event, final WatchKey key) { final Object obj = event.context(); - Console.log("创建:{}-> {}", currentPath, obj); + Console.log("创建:{}-> {}", key.watchable(), obj); } @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { final Object obj = event.context(); - Console.log("修改:{}-> {}", currentPath, obj); + Console.log("修改:{}-> {}", key.watchable(), obj); } @Override - public void onDelete(final WatchEvent event, final Path currentPath) { + public void onDelete(final WatchEvent event, final WatchKey key) { final Object obj = event.context(); - Console.log("删除:{}-> {}", currentPath, obj); + Console.log("删除:{}-> {}", key.watchable(), obj); } @Override - public void onOverflow(final WatchEvent event, final Path currentPath) { + public void onOverflow(final WatchEvent event, final WatchKey key) { final Object obj = event.context(); - Console.log("Overflow:{}-> {}", currentPath, obj); + Console.log("Overflow:{}-> {}", key.watchable(), obj); } }; diff --git a/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java b/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java index 7c6409368..ea2d403ab 100644 --- a/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java +++ b/hutool-setting/src/main/java/org/dromara/hutool/setting/Setting.java @@ -14,18 +14,18 @@ package org.dromara.hutool.setting; import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.convert.Convert; -import org.dromara.hutool.core.io.file.FileUtil; -import org.dromara.hutool.core.io.IoUtil; -import org.dromara.hutool.core.io.resource.Resource; -import org.dromara.hutool.core.io.resource.ResourceUtil; -import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; -import org.dromara.hutool.core.io.watch.WatchMonitor; -import org.dromara.hutool.core.io.watch.WatchUtil; -import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.func.LambdaUtil; import org.dromara.hutool.core.func.SerSupplier; -import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.io.IoUtil; +import org.dromara.hutool.core.io.file.FileUtil; +import org.dromara.hutool.core.io.resource.Resource; +import org.dromara.hutool.core.io.resource.ResourceUtil; +import org.dromara.hutool.core.io.watch.WatchMonitor; +import org.dromara.hutool.core.io.watch.WatchUtil; +import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; +import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.text.CharUtil; +import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.log.LogUtil; import org.dromara.hutool.setting.props.Props; @@ -33,15 +33,9 @@ import org.dromara.hutool.setting.props.Props; import java.io.File; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.Path; import java.nio.file.WatchEvent; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; +import java.nio.file.WatchKey; +import java.util.*; import java.util.function.Consumer; /** @@ -217,13 +211,11 @@ public class Setting extends AbsSetting implements Map { public void autoLoad(final boolean autoReload, final Consumer callback) { if (autoReload) { Assert.notNull(this.resource, "Setting resource must be not null !"); - if (null != this.watchMonitor) { - // 先关闭之前的监听 - this.watchMonitor.close(); - } + // 先关闭之前的监听 + IoUtil.closeQuietly(this.watchMonitor); this.watchMonitor = WatchUtil.createModify(resource.getUrl(), new SimpleWatcher() { @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { final boolean success = load(); // 如果有回调,加载完毕则执行回调 if (callback != null) { diff --git a/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java b/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java index 5ffd52d48..68aaf3bb7 100644 --- a/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java +++ b/hutool-setting/src/main/java/org/dromara/hutool/setting/props/Props.java @@ -22,9 +22,9 @@ import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.io.resource.Resource; import org.dromara.hutool.core.io.resource.ResourceUtil; -import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; import org.dromara.hutool.core.io.watch.WatchMonitor; import org.dromara.hutool.core.io.watch.WatchUtil; +import org.dromara.hutool.core.io.watch.watchers.SimpleWatcher; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.getter.TypeGetter; import org.dromara.hutool.core.map.MapUtil; @@ -39,8 +39,8 @@ import java.io.IOException; import java.io.Writer; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.Path; import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; import java.util.Arrays; import java.util.Properties; @@ -234,7 +234,7 @@ public final class Props extends Properties implements TypeGetter } this.watchMonitor = WatchUtil.createModify(this.resource.getUrl(), new SimpleWatcher() { @Override - public void onModify(final WatchEvent event, final Path currentPath) { + public void onModify(final WatchEvent event, final WatchKey key) { load(); } });