本文整理汇总了Java中java.nio.file.WatchKey.cancel方法的典型用法代码示例。如果您正苦于以下问题:Java WatchKey.cancel方法的具体用法?Java WatchKey.cancel怎么用?Java WatchKey.cancel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.WatchKey
的用法示例。
在下文中一共展示了WatchKey.cancel方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
protected void run() {
while(isRunning()) {
try {
final WatchKey key = watchService.take();
final WatchedDirectory watchedDirectory = dirsByKey.get(key);
if(watchedDirectory == null) {
logger.warning("Cancelling unknown key " + key);
key.cancel();
} else {
for(WatchEvent<?> event : key.pollEvents()) {
watchedDirectory.dispatch((WatchEvent<Path>) event);
}
key.reset();
}
} catch(InterruptedException e) {
// ignore, just check for termination
}
}
}
示例2: manageDirectoryDeletion
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void manageDirectoryDeletion(Path filename) throws IOException {
PhotatoFolder parentFolder = getCurrentFolder(filename.getParent());
parentFolder.subFolders.remove(filename.getFileName().toString());
WatchKey removed = watchedDirectoriesKeys.remove(filename);
if (removed != null) {
removed.cancel();
watchedDirectoriesPaths.remove(removed);
}
PhotatoFolder currentFolder = getCurrentFolder(filename);
if (currentFolder.medias != null) {
for (PhotatoMedia media : currentFolder.medias) {
try {
searchManager.removeMedia(media);
albumsManager.removeMedia(media);
thumbnailGenerator.deleteThumbnail(media.fsPath, media.lastModificationTimestamp);
fullScreenImageGetter.deleteImage(media);
} catch (IOException ex) {
}
}
}
}
示例3: handle
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(Path dir, WatchService watcher) {
try {
try {
Path file = dir.resolve("anyfile");
for (int i=0; i<2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
watcher.close();
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
}
示例4: handle
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(int id, Path dir, WatchService watcher) {
System.out.printf("begin handle %d%n", id);
try {
try {
Path file = dir.resolve("anyfile");
for (int i=0; i<2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
System.out.printf("WatchService %d closing ...%n", id);
watcher.close();
System.out.printf("WatchService %d closed %n", id);
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
System.out.printf("end handle %d%n", id);
}
示例5: close
import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
public void close() throws IOException
{
ArrayList<JWatchKey> watchList = new ArrayList<>();
if (_isClosed.getAndSet(true)) {
return;
}
watchList.addAll(_watchList);
_watchList.clear();
for (WatchKey key : watchList) {
key.cancel();
}
}
示例6: removeWatch
import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
protected void removeWatch(WatchKey key) throws IOException {
try {
key.cancel();
} catch (ClosedWatchServiceException ex) {
throw new IOException(ex);
}
}
示例7: cancel
import java.nio.file.WatchKey; //导入方法依赖的package包/类
public synchronized void cancel(Path path) {
WatchKey watchKey = pathWatchKeyMap.get(path);
if (watchKey != null) {
pathWatchKeyMap.remove(path);
watchKey.cancel();
}
}
示例8: clear
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
* Clears all previously registered watched folders.
*/
public void clear() {
synchronized ( keyInfoMap ) {
for ( final WatchKey key : keyInfoMap.keySet() )
key.cancel();
keyInfoMap.clear();
}
}
示例9: monitor
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void monitor(File file) throws Exception{
watchService = FileSystems.getDefault().newWatchService();
file.toPath().register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE);
while(running){
WatchKey watchKey = watchService.take();
List<WatchEvent<?>> events = watchKey.pollEvents();
if(events != null){
for(WatchEvent<?> e : events){
//事件处理
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>)e;
final Path filename = watchEventPath.context();
if(e.kind() == StandardWatchEventKinds.ENTRY_CREATE){
addDataSource(filename.toString());
}else if(e.kind() == StandardWatchEventKinds.ENTRY_DELETE){
removeDataSource(filename.toString());
}
}
}
if(!watchKey.reset()){
watchKey.cancel();
logger.error("移除dataSource文件目录监控服务");
break;
}
}
running = false;
watchService.close();
}
示例10: monitor
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void monitor(Path path) throws Exception{
watchService = FileSystems.getDefault().newWatchService();
registerTree(path);
while(running){
WatchKey watchKey = watchService.take();
List<WatchEvent<?>> events = watchKey.pollEvents();
if(events != null){
for(WatchEvent<?> e : events){
//事件处理
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>)e;
final Path filename = watchEventPath.context();
final Path directory_path = directories.get(watchKey);
final Path child = directory_path.resolve(filename);
boolean isChildDirectory = Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS);
if(e.kind() == StandardWatchEventKinds.ENTRY_CREATE){
if(isChildDirectory){
registerTree(child);
}else{
updateSqlFile(child.toFile(),false);
}
logger.info("新增文件:"+child.toAbsolutePath());
}else if(e.kind() == StandardWatchEventKinds.ENTRY_MODIFY){
boolean isExsist = child.toFile().exists();
if(isExsist && !isChildDirectory){
updateSqlFile(child.toFile(),false);
logger.info("更新sql文件:"+child.toAbsolutePath());
}
}else if(e.kind() == StandardWatchEventKinds.ENTRY_DELETE){
updateSqlFile(child.toFile(),true);
logger.info("删除文件:"+child.toAbsolutePath());
}
}
}
if(!watchKey.reset()){
Path pth = directories.remove(watchKey);
watchKey.cancel();
logger.error("移除sql文件目录"+pth.toString()+"监控服务");
if(directories.isEmpty()){
break;
}
}
}
running = false;
watchService.close();
}