本文整理汇总了Java中java.nio.file.WatchKey.isValid方法的典型用法代码示例。如果您正苦于以下问题:Java WatchKey.isValid方法的具体用法?Java WatchKey.isValid怎么用?Java WatchKey.isValid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.WatchKey
的用法示例。
在下文中一共展示了WatchKey.isValid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: watchDir
import java.nio.file.WatchKey; //导入方法依赖的package包/类
protected void watchDir(Path dir) throws IOException {
LOG.debug("Registering watch for {}", dir);
if (Thread.currentThread().isInterrupted()) {
LOG.debug("Skipping adding watch since current thread is interrupted.");
}
// check if directory is already watched
// on Windows, check if any parent is already watched
for (Path path = dir; path != null; path = FILE_TREE_WATCHING_SUPPORTED ? path.getParent() : null) {
WatchKey previousWatchKey = watchKeys.get(path);
if (previousWatchKey != null && previousWatchKey.isValid()) {
LOG.debug("Directory {} is already watched and the watch is valid, not adding another one.", path);
return;
}
}
int retryCount = 0;
IOException lastException = null;
while (retryCount++ < 2) {
try {
WatchKey watchKey = dir.register(watchService, WATCH_KINDS, WATCH_MODIFIERS);
watchKeys.put(dir, watchKey);
return;
} catch (IOException e) {
LOG.debug("Exception in registering for watching of " + dir, e);
lastException = e;
if (e instanceof NoSuchFileException) {
LOG.debug("Return silently since directory doesn't exist.");
return;
}
if (e instanceof FileSystemException && e.getMessage() != null && e.getMessage().contains("Bad file descriptor")) {
// retry after getting "Bad file descriptor" exception
LOG.debug("Retrying after 'Bad file descriptor'");
continue;
}
// Windows at least will sometimes throw odd exceptions like java.nio.file.AccessDeniedException
// if the file gets deleted while the watch is being set up.
// So, we just ignore the exception if the dir doesn't exist anymore
if (!Files.exists(dir)) {
// return silently when directory doesn't exist
LOG.debug("Return silently since directory doesn't exist.");
return;
} else {
// no retry
throw e;
}
}
}
LOG.debug("Retry count exceeded, throwing last exception");
throw lastException;
}
示例2: filesFromEvents
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private Set<File> filesFromEvents() throws InterruptedException {
WatchKey key = watcher.take();
Set<File> files = new LinkedHashSet<File>();
if (key != null && key.isValid())
{
for (WatchEvent<?> event : key.pollEvents())
{
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY)
{
Path item = (Path) event.context();
File file = new File(((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName());
if (log.isDebugEnabled()) {
log.debug("Watch Event: " + event.kind() + ": " + file);
}
if(isJarFile(file))
{
files.add(file);
}
else
log.warn("[JAR Loader] Ignoring file "+file);
}
}
key.reset();
}
return files;
}