public class WatchServer extends Thread implements Closeable, Serializable
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
protected WatchEvent.Kind<?>[] |
events
监听事件列表
|
protected boolean |
isClosed
监听是否已经关闭
|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
WatchServer() |
Modifier and Type | Method and Description |
---|---|
void |
close()
关闭监听
|
WatchKey |
getWatchKey(Path path)
通过 path 获取 watchKey
|
void |
init()
初始化
初始化包括: 1、解析传入的路径,判断其为目录还是文件 2、创建 WatchService 对象
|
void |
registerPath(Path path,
int maxDepth)
将指定路径加入到监听中
|
void |
setModifiers(WatchEvent.Modifier[] modifiers)
设置监听选项,例如监听频率等,可设置项包括:
1、com.sun.nio.file.StandardWatchEventKinds
2、com.sun.nio.file.SensitivityWatchEventModifier
|
void |
watch(WatchAction action,
Filter<WatchEvent<?>> watchFilter)
执行事件获取并处理
|
void |
watch(Watcher watcher,
Filter<WatchEvent<?>> watchFilter)
执行事件获取并处理
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
protected WatchEvent.Kind<?>[] events
protected boolean isClosed
public void init() throws WatchException
1、解析传入的路径,判断其为目录还是文件
2、创建WatchService
对象
WatchException
- 监听异常,IO异常时抛出此异常public void setModifiers(WatchEvent.Modifier[] modifiers)
1、com.sun.nio.file.StandardWatchEventKinds 2、com.sun.nio.file.SensitivityWatchEventModifier
modifiers
- 监听选项,例如监听频率等public void registerPath(Path path, int maxDepth)
path
- 路径maxDepth
- 递归下层目录的最大深度public void watch(WatchAction action, Filter<WatchEvent<?>> watchFilter)
action
- 监听回调函数,实现此函数接口用于处理WatchEvent事件watchFilter
- 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤public void watch(Watcher watcher, Filter<WatchEvent<?>> watchFilter)
watcher
- Watcher
watchFilter
- 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,null表示不过滤public WatchKey getWatchKey(Path path)
path
- pathpublic void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2024. All rights reserved.