gpt4 book ai didi

java - 不断阅读java WatchEvents

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:48 25 4
gpt4 key购买 nike

我正在尝试在客户端和服务器之间同步两个文件夹及其子目录。我有this class的修改版本我在下面发布了。在我的 Client 类中,我创建一个 WatchDir 对象并在无限循环中调用其 processEvents() 方法。

如果事件已注册,则该方法返回一个 myTuple 对象(一个包含事件类型和路径对象的结构),否则返回 null。问题是,这似乎只适用于目录中发生的第一个事件(即,如果我将文件添加到监视文件夹中,我的 WatchDir object.processEvents() 返回一个带有 ENTRY_CREATE 事件,并且永远不会为之后发生的其他文件添加/删除/修改返回另一个元组)。我希望每次发生某个事件时都会连续调用 processEvents (因此是无限的 while),返回一个元组。

我修改后的WatchDir:

import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class WatchDir {
private final WatchService watcher;
private final Map<WatchKey,Path> keys;
private final boolean recursive;
private boolean trace = false;

public WatchDir(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey,Path>(); //holds the key for each subdirectory
this.recursive = true;

registerAll(dir);
}

public void registerAll(Path start) throws IOException {
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
register(dir);
return FileVisitResult.CONTINUE;
}
});
}

public void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
keys.put(key, dir);
}

public myTuple processEvents() {
WatchKey key;
//while (true) {

try {
key = watcher.take();
} catch (InterruptedException e) {
return new myTuple("INTERRUPTED", null);
}

Path dir = keys.get(key); //get next subdirectory path
if (dir == null)
return new myTuple("NULL DIRECTORY", null);

for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);

return new myTuple(event.kind().name(), child);
}
return null;
//}
}

@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
}

我的客户:

import java.nio.file.attribute.*;
import java.nio.file.*;
import java.util.concurrent.TimeUnit;

public class newClient {
public static void main(String[] args) throws IOException {

Path folder = Paths.get(System.getProperty("user.dir"));
WatchDir watcher = new WatchDir(folder, true);
myTuple thisTuple;

while (true) {
thisTuple = watcher.processEvents();
String event = thisTuple.getEvent();
Path path = thisTuple.getPath();

System.out.println(event+": "+path.toString());
}
}
}

最佳答案

您没有重置 key 。阅读docs再次:

Once the events have been processed the consumer invokes the key's reset method to reset the key which allows the key to be signalled and re-queued with further events.

大概在这里

       for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);

return new myTuple(event.kind().name(), child);
}
key.reset();
return null;

关于java - 不断阅读java WatchEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com