Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions common-legacy-api/src/main/java/taboolib/common5/FileWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import taboolib.common.LifeCycle;
import taboolib.common.PrimitiveIO;
import taboolib.common.TabooLib;
import taboolib.common.platform.Ghost;

Expand Down Expand Up @@ -50,8 +51,18 @@ public class FileWatcher {
private final WatchService watchService;

public FileWatcher(int interval) {
WatchService ws;
try {
this.watchService = FileSystems.getDefault().newWatchService();
ws = FileSystems.getDefault().newWatchService();
} catch (IOException e) {
PrimitiveIO.warning(PrimitiveIO.t(
"FileWatcher 初始化失败,文件自动重载功能不可用: " + e.getMessage(),
"FileWatcher failed to initialize, auto-reload is unavailable: " + e.getMessage()
));
ws = null;
}
this.watchService = ws;
if (this.watchService != null) {
this.executorService.scheduleAtFixedRate(() -> {
WatchKey key;
while ((key = watchService.poll()) != null) {
Expand All @@ -76,8 +87,6 @@ public FileWatcher(int interval) {
}, 1000, interval, TimeUnit.MILLISECONDS);
// 注册关闭回调
TabooLib.registerLifeCycleTask(LifeCycle.DISABLE, 0, this::release);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -99,6 +108,9 @@ public void addSimpleListener(File file, Consumer<File> runnable) {
* @param runImmediately 是否在添加监听器时立即执行一次
*/
public void addSimpleListener(File file, Consumer<File> runnable, boolean runImmediately) {
if (watchService == null) {
return;
}
if (runImmediately) {
runnable.accept(file);
}
Expand Down
Loading