public class WatchMonitor extends WatchServer
监听器可监听目录或文件
如果监听的Path不存在,则递归创建空目录然后监听此空目录
递归监听目录时,并不会监听新创建的目录
Thread.State, Thread.UncaughtExceptionHandler
Modifier and Type | Field and Description |
---|---|
static WatchEvent.Kind<?> |
ENTRY_CREATE
创建事件
|
static WatchEvent.Kind<?> |
ENTRY_DELETE
删除事件
|
static WatchEvent.Kind<?> |
ENTRY_MODIFY
修改事件
|
static WatchEvent.Kind<?>[] |
EVENTS_ALL
全部事件
|
static WatchEvent.Kind<?> |
OVERFLOW
事件丢失
|
events, isClosed
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
Constructor and Description |
---|
WatchMonitor(File file,
WatchEvent.Kind<?>... events)
构造
|
WatchMonitor(Path path,
int maxDepth,
WatchEvent.Kind<?>... events)
构造
例如设置: maxDepth <= 1 表示只监听当前目录 maxDepth = 2 表示监听当前目录以及下层目录 maxDepth = 3 表示监听当前目录以及下两层 |
WatchMonitor(Path path,
WatchEvent.Kind<?>... events)
构造
|
WatchMonitor(String path,
WatchEvent.Kind<?>... events)
构造
|
Modifier and Type | Method and Description |
---|---|
static WatchMonitor |
create(File file,
int maxDepth,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(File file,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(Path path,
int maxDepth,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(Path path,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(String path,
int maxDepth,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(String path,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(URI uri,
int maxDepth,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(URI uri,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(URL url,
int maxDepth,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
create(URL url,
WatchEvent.Kind<?>... events)
创建并初始化监听
|
static WatchMonitor |
createAll(File file,
Watcher watcher)
创建并初始化监听,监听所有事件
|
static WatchMonitor |
createAll(Path path,
Watcher watcher)
创建并初始化监听,监听所有事件
|
static WatchMonitor |
createAll(String path,
Watcher watcher)
创建并初始化监听,监听所有事件
|
static WatchMonitor |
createAll(URI uri,
Watcher watcher)
创建并初始化监听,监听所有事件
|
static WatchMonitor |
createAll(URL url,
Watcher watcher)
创建并初始化监听,监听所有事件
|
void |
init()
初始化
初始化包括: 1、解析传入的路径,判断其为目录还是文件 2、创建 WatchService 对象
|
void |
run() |
WatchMonitor |
setMaxDepth(int maxDepth)
当监听目录时,监听目录的最大深度
当设置值为1(或小于1)时,表示不递归监听子目录 例如设置: maxDepth <= 1 表示只监听当前目录 maxDepth = 2 表示监听当前目录以及下层目录 maxDepth = 3 表示监听当前目录以及下层 |
WatchMonitor |
setWatcher(Watcher watcher)
设置监听
多个监听请使用 WatcherChain |
void |
watch()
开始监听事件,阻塞当前进程
|
void |
watch(Watcher watcher)
开始监听事件,阻塞当前进程
|
close, getWatchKey, registerPath, setModifiers, watch, watch
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, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public static final WatchEvent.Kind<?> OVERFLOW
public static final WatchEvent.Kind<?> ENTRY_MODIFY
public static final WatchEvent.Kind<?> ENTRY_CREATE
public static final WatchEvent.Kind<?> ENTRY_DELETE
public static final WatchEvent.Kind<?>[] EVENTS_ALL
public WatchMonitor(File file, WatchEvent.Kind<?>... events)
file
- 文件events
- 监听的事件列表public WatchMonitor(String path, WatchEvent.Kind<?>... events)
path
- 字符串路径events
- 监听的事件列表public WatchMonitor(Path path, WatchEvent.Kind<?>... events)
path
- 字符串路径events
- 监听事件列表public WatchMonitor(Path path, int maxDepth, WatchEvent.Kind<?>... events)
maxDepth <= 1 表示只监听当前目录 maxDepth = 2 表示监听当前目录以及下层目录 maxDepth = 3 表示监听当前目录以及下两层
path
- 字符串路径maxDepth
- 递归目录的最大深度,当小于2时不递归下层目录events
- 监听事件列表public static WatchMonitor create(URL url, WatchEvent.Kind<?>... events)
url
- URLevents
- 监听的事件列表public static WatchMonitor create(URL url, int maxDepth, WatchEvent.Kind<?>... events)
url
- URLevents
- 监听的事件列表maxDepth
- 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录public static WatchMonitor create(URI uri, WatchEvent.Kind<?>... events)
uri
- URIevents
- 监听的事件列表public static WatchMonitor create(URI uri, int maxDepth, WatchEvent.Kind<?>... events)
uri
- URIevents
- 监听的事件列表maxDepth
- 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录public static WatchMonitor create(File file, WatchEvent.Kind<?>... events)
file
- 文件events
- 监听的事件列表public static WatchMonitor create(File file, int maxDepth, WatchEvent.Kind<?>... events)
file
- 文件events
- 监听的事件列表maxDepth
- 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录public static WatchMonitor create(String path, WatchEvent.Kind<?>... events)
path
- 路径events
- 监听的事件列表public static WatchMonitor create(String path, int maxDepth, WatchEvent.Kind<?>... events)
path
- 路径events
- 监听的事件列表maxDepth
- 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录public static WatchMonitor create(Path path, WatchEvent.Kind<?>... events)
path
- 路径events
- 监听事件列表public static WatchMonitor create(Path path, int maxDepth, WatchEvent.Kind<?>... events)
path
- 路径events
- 监听事件列表maxDepth
- 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录public static WatchMonitor createAll(URI uri, Watcher watcher)
uri
- URIwatcher
- Watcher
public static WatchMonitor createAll(URL url, Watcher watcher)
url
- URLwatcher
- Watcher
public static WatchMonitor createAll(File file, Watcher watcher)
file
- 被监听文件watcher
- Watcher
public static WatchMonitor createAll(String path, Watcher watcher)
path
- 路径watcher
- Watcher
public static WatchMonitor createAll(Path path, Watcher watcher)
path
- 路径watcher
- Watcher
public void init() throws WatchException
1、解析传入的路径,判断其为目录还是文件
2、创建WatchService
对象
init
in class WatchServer
WatchException
- 监听异常,IO异常时抛出此异常public WatchMonitor setWatcher(Watcher watcher)
WatcherChain
watcher
- 监听public void watch()
public void watch(Watcher watcher) throws WatchException
watcher
- 监听WatchException
- 监听异常,如果监听关闭抛出此异常public WatchMonitor setMaxDepth(int maxDepth)
maxDepth <= 1 表示只监听当前目录 maxDepth = 2 表示监听当前目录以及下层目录 maxDepth = 3 表示监听当前目录以及下层
maxDepth
- 最大深度,当设置值为1(或小于1)时,表示不递归监听子目录,监听所有子目录请传Integer.MAX_VALUE
Copyright © 2024. All rights reserved.