mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add WatchAction
This commit is contained in:
parent
758626a482
commit
0dcf4f9aca
@ -10,6 +10,7 @@
|
|||||||
* 【core 】 StrUtil增加filter方法(pr#149@Gitee)
|
* 【core 】 StrUtil增加filter方法(pr#149@Gitee)
|
||||||
* 【core 】 DateUtil增加beginOfWeek重载
|
* 【core 】 DateUtil增加beginOfWeek重载
|
||||||
* 【core 】 将有歧义的BeanUtil.mapToBean方法置为过期(使用toBean方法)
|
* 【core 】 将有歧义的BeanUtil.mapToBean方法置为过期(使用toBean方法)
|
||||||
|
* 【core 】 添加WatchAction(对Watcher的抽象)
|
||||||
|
|
||||||
### Bug修复#
|
### Bug修复#
|
||||||
* 【core 】 修复原始类型转换时,转换失败没有抛出异常的问题
|
* 【core 】 修复原始类型转换时,转换失败没有抛出异常的问题
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package cn.hutool.core.io.watch;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.WatchEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听事件处理函数接口
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.4.0
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface WatchAction {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事件处理,通过实现此方法处理各种事件。
|
||||||
|
*
|
||||||
|
* 事件可以调用 {@link WatchEvent#kind()}获取,对应事件见{@link WatchKind}
|
||||||
|
*
|
||||||
|
* @param event 事件
|
||||||
|
* @param currentPath 事件发生的当前Path路径
|
||||||
|
*/
|
||||||
|
void doAction(WatchEvent<?> event, Path currentPath);
|
||||||
|
}
|
@ -129,10 +129,11 @@ public class WatchServer extends Thread implements Closeable, Serializable {
|
|||||||
/**
|
/**
|
||||||
* 执行事件获取并处理
|
* 执行事件获取并处理
|
||||||
*
|
*
|
||||||
* @param watcher {@link Watcher}
|
* @param action 监听回调函数,实现此函数接口用于处理WatchEvent事件
|
||||||
* @param watchFilter 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤
|
* @param watchFilter 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤
|
||||||
|
* @since 5.4.0
|
||||||
*/
|
*/
|
||||||
public void watch(Watcher watcher, Filter<WatchEvent<?>> watchFilter) {
|
public void watch(WatchAction action, Filter<WatchEvent<?>> watchFilter) {
|
||||||
WatchKey wk;
|
WatchKey wk;
|
||||||
try {
|
try {
|
||||||
wk = watchService.take();
|
wk = watchService.take();
|
||||||
@ -142,15 +143,28 @@ public class WatchServer extends Thread implements Closeable, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Path currentPath = watchKeyPathMap.get(wk);
|
final Path currentPath = watchKeyPathMap.get(wk);
|
||||||
WatchEvent.Kind<?> kind;
|
|
||||||
for (WatchEvent<?> event : wk.pollEvents()) {
|
|
||||||
kind = event.kind();
|
|
||||||
|
|
||||||
|
for (WatchEvent<?> event : wk.pollEvents()) {
|
||||||
// 如果监听文件,检查当前事件是否与所监听文件关联
|
// 如果监听文件,检查当前事件是否与所监听文件关联
|
||||||
if (null != watchFilter && false == watchFilter.accept(event)) {
|
if (null != watchFilter && false == watchFilter.accept(event)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
action.doAction(event, currentPath);
|
||||||
|
}
|
||||||
|
wk.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行事件获取并处理
|
||||||
|
*
|
||||||
|
* @param watcher {@link Watcher}
|
||||||
|
* @param watchFilter 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤
|
||||||
|
*/
|
||||||
|
public void watch(Watcher watcher, Filter<WatchEvent<?>> watchFilter) {
|
||||||
|
watch((event, currentPath)->{
|
||||||
|
WatchEvent.Kind<?> kind = event.kind();
|
||||||
|
|
||||||
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
|
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
|
||||||
watcher.onCreate(event, currentPath);
|
watcher.onCreate(event, currentPath);
|
||||||
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
|
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
|
||||||
@ -160,8 +174,7 @@ public class WatchServer extends Thread implements Closeable, Serializable {
|
|||||||
} else if (kind == StandardWatchEventKinds.OVERFLOW) {
|
} else if (kind == StandardWatchEventKinds.OVERFLOW) {
|
||||||
watcher.onOverflow(event, currentPath);
|
watcher.onOverflow(event, currentPath);
|
||||||
}
|
}
|
||||||
}
|
}, watchFilter);
|
||||||
wk.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user