diff --git a/CHANGELOG.md b/CHANGELOG.md index 045faffde..f0cc3e297 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.19.M1 (2023-05-20) +# 5.8.19.M1 (2023-05-23) ### 🐣新特性 * 【db 】 优化HttpRequest.toString()内容打印(issue#3072@Github) @@ -14,6 +14,7 @@ * 【core 】 SyncFinisher增加setExceptionHandler方法(issue#I716SX@Gitee) * 【core 】 FileTypeUtil.getType增加文件判断(pr#3112@Github) * 【core 】 增加CsvWriteConfig.setEndingLineBreak配置项(issue#I75K5G@Gitee) +* 【core 】 增加Tailer追踪文件时文件被删除的处理情况(pr#3115@Github) ### 🐞Bug修复 * 【core 】 修复URLUtil.decode无法解码UTF-16问题(issue#3063@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/Tailer.java b/hutool-core/src/main/java/cn/hutool/core/io/file/Tailer.java index 8c5d87740..5e7164bfe 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/Tailer.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/Tailer.java @@ -6,6 +6,8 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.LineHandler; +import cn.hutool.core.io.watch.SimpleWatcher; +import cn.hutool.core.io.watch.WatchKind; import cn.hutool.core.io.watch.WatchMonitor; import cn.hutool.core.lang.Console; import cn.hutool.core.util.CharUtil; @@ -46,11 +48,10 @@ public class Tailer implements Serializable { private final long period; private final String filePath; - private final RandomAccessFile randomAccessFile; private final ScheduledExecutorService executorService; - private WatchMonitor fileDeleteWatchMonitor; + private boolean stopOnDelete; /** * 构造,默认UTF-8编码 @@ -104,6 +105,15 @@ public class Tailer implements Serializable { this.filePath=file.getAbsolutePath(); } + /** + * 设置删除文件后是否退出并抛出异常 + * + * @param stopOnDelete 删除文件后是否退出并抛出异常 + */ + public void setStopOnDelete(final boolean stopOnDelete) { + this.stopOnDelete = stopOnDelete; + } + /** * 开始监听 */ @@ -124,25 +134,26 @@ public class Tailer implements Serializable { throw new IORuntimeException(e); } - final LineReadWatcher lineReadWatcher = new LineReadWatcher(this.randomAccessFile, this.charset, this.lineHandler){ - @Override - public void onDelete(WatchEvent event, Path currentPath) { - super.onDelete(event, currentPath); - Path deletedPath = (Path) event.context(); - if (deletedPath.toString().equals(FileUtil.file(filePath).getName())) { - stop(); - throw new IORuntimeException("{} has been deleted", filePath); - } - } - }; + final LineReadWatcher lineReadWatcher = new LineReadWatcher(this.randomAccessFile, this.charset, this.lineHandler); final ScheduledFuture scheduledFuture = this.executorService.scheduleAtFixedRate(// lineReadWatcher, // 0, // this.period, TimeUnit.MILLISECONDS// ); - fileDeleteWatchMonitor = WatchMonitor.create(this.filePath, WatchMonitor.ENTRY_DELETE); - fileDeleteWatchMonitor.setWatcher(lineReadWatcher); - fileDeleteWatchMonitor.start(); + + // 监听删除 + if(stopOnDelete){ + fileDeleteWatchMonitor = WatchMonitor.create(this.filePath, WatchKind.DELETE.getValue()); + fileDeleteWatchMonitor.setWatcher(new SimpleWatcher(){ + @Override + public void onDelete(final WatchEvent event, final Path currentPath) { + super.onDelete(event, currentPath); + stop(); + throw new IORuntimeException("{} has been deleted", filePath); + } + }); + fileDeleteWatchMonitor.start(); + } if (false == async) { try {