mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-15 23:13:33 +08:00
fix code
This commit is contained in:
parent
3c42b0c164
commit
0c09c5f101
@ -16,6 +16,9 @@ import org.dromara.hutool.core.date.DateUnit;
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.watch.SimpleWatcher;
|
||||
import org.dromara.hutool.core.io.watch.WatchKind;
|
||||
import org.dromara.hutool.core.io.watch.WatchMonitor;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.func.SerConsumer;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
@ -26,6 +29,8 @@ 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.util.Stack;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -56,8 +61,12 @@ 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编码
|
||||
@ -102,6 +111,7 @@ public class Tailer implements Serializable {
|
||||
*/
|
||||
public Tailer(final File file, final Charset charset, final SerConsumer<String> lineHandler, final int initReadLine, final long period) {
|
||||
checkFile(file);
|
||||
this.filePath = file.getAbsolutePath();
|
||||
this.charset = charset;
|
||||
this.lineHandler = lineHandler;
|
||||
this.period = period;
|
||||
@ -110,6 +120,15 @@ public class Tailer implements Serializable {
|
||||
this.executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置删除文件后是否退出并抛出异常
|
||||
*
|
||||
* @param stopOnDelete 删除文件后是否退出并抛出异常
|
||||
*/
|
||||
public void setStopOnDelete(final boolean stopOnDelete) {
|
||||
this.stopOnDelete = stopOnDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监听
|
||||
*/
|
||||
@ -137,6 +156,20 @@ public class Tailer implements Serializable {
|
||||
this.period, TimeUnit.MILLISECONDS//
|
||||
);
|
||||
|
||||
// 监听删除
|
||||
if(stopOnDelete){
|
||||
fileDeleteWatchMonitor = WatchMonitor.of(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 (!async) {
|
||||
try {
|
||||
scheduledFuture.get();
|
||||
@ -156,6 +189,7 @@ public class Tailer implements Serializable {
|
||||
this.executorService.shutdown();
|
||||
} finally {
|
||||
IoUtil.closeQuietly(this.randomAccessFile);
|
||||
fileDeleteWatchMonitor.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param url URL
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor of(final URL url, final WatchEvent.Kind<?>... events) {
|
||||
@ -72,7 +72,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param url URL
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
@ -84,7 +84,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param uri URI
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor of(final URI uri, final WatchEvent.Kind<?>... events) {
|
||||
@ -95,7 +95,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param uri URI
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
@ -107,7 +107,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param file 文件
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor of(final File file, final WatchEvent.Kind<?>... events) {
|
||||
@ -118,7 +118,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param file 文件
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
@ -130,7 +130,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param path 路径
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor of(final String path, final WatchEvent.Kind<?>... events) {
|
||||
@ -141,7 +141,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param path 路径
|
||||
* @param events 监听的事件列表
|
||||
* @param events 监听的事件列表,见{@link WatchKind}
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
@ -153,7 +153,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param path 路径
|
||||
* @param events 监听事件列表
|
||||
* @param events 监听事件列表,见{@link WatchKind}
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor of(final Path path, final WatchEvent.Kind<?>... events) {
|
||||
@ -164,7 +164,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* 创建并初始化监听
|
||||
*
|
||||
* @param path 路径
|
||||
* @param events 监听事件列表
|
||||
* @param events 监听事件列表,见{@link WatchKind}
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user