gpt4 book ai didi

java - WatchService 有时会触发 ENTRY_MODIFY 两次,有时会触发一次

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:08 28 4
gpt4 key购买 nike

我正在使用来自 Oracle 的这个 WatchService 示例:

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

public class WatchDir {

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

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

/**
* Register the given directory with the WatchService
*/
private void register(Path dir) throws IOException {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
if (trace) {
Path prev = keys.get(key);
if (prev == null) {
System.out.format("register: %s\n", dir);
} else {
if (!dir.equals(prev)) {
System.out.format("update: %s -> %s\n", prev, dir);
}
}
}
keys.put(key, dir);
}

/**
* Register the given directory, and all its sub-directories, with the
* WatchService.
*/
private void registerAll(final Path start) throws IOException {
// register directory and sub-directories
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException
{
register(dir);
return FileVisitResult.CONTINUE;
}
});
}

/**
* Creates a WatchService and registers the given directory
*/
WatchDir(Path dir, boolean recursive) throws IOException {
this.watcher = FileSystems.getDefault().newWatchService();
this.keys = new HashMap<WatchKey,Path>();
this.recursive = recursive;

if (recursive) {
System.out.format("Scanning %s ...\n", dir);
registerAll(dir);
System.out.println("Done.");
} else {
register(dir);
}

// enable trace after initial registration
this.trace = true;
}

/**
* Process all events for keys queued to the watcher
*/
void processEvents() {
for (;;) {

// wait for key to be signalled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}

Path dir = keys.get(key);
if (dir == null) {
System.err.println("WatchKey not recognized!!");
continue;
}

for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind kind = event.kind();

// TBD - provide example of how OVERFLOW event is handled
if (kind == OVERFLOW) {
continue;
}

// Context for directory entry event is the file name of entry
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);

// print out event
System.out.format("%s: %s\n", event.kind().name(), child);

// if directory is created, and watching recursively, then
// register it and its sub-directories
if (recursive && (kind == ENTRY_CREATE)) {
try {
if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException x) {
// ignore to keep sample readbale
}
}
}

// reset key and remove from set if directory no longer accessible
boolean valid = key.reset();
if (!valid) {
keys.remove(key);

// all directories are inaccessible
if (keys.isEmpty()) {
break;
}
}
}
}

static void usage() {
System.err.println("usage: java WatchDir [-r] dir");
System.exit(-1);
}

public static void main(String[] args) throws IOException {
// parse arguments
if (args.length == 0 || args.length > 2)
usage();
boolean recursive = false;
int dirArg = 0;
if (args[0].equals("-r")) {
if (args.length < 2)
usage();
recursive = true;
dirArg++;
}

// register directory and process its events
Path dir = Paths.get(args[dirArg]);
new WatchDir(dir, recursive).processEvents();
}
}

我正在Windows 7开发一个应用程序,部署环境是rhel 7.2。起初,在这两个操作系统中,每当我复制一个文件时,它都会触发一个 ENTRY_CREATED,然后是两个 ENTRY_MODIFY。第一个 ENTRY_MODIFY 在复制开始时,第二个 ENTRY_MODIFY 在复制结束时。所以我能够理解复制过程已经结束。但是,它现在只在 rhel 7.2 中触发一个 ENTRY_MODIFY。不过,它仍然会在 Windows 7 中触发两个 ENTRY_MODIFY 事件。

我找到了 thisstackoverflow 中。这个问题问为什么两个 ENTRY_MODIFY 被触发。这不完全是我的问题,但其中一个答案与我的要求存在争议。可悲的是,我的问题在那场争端中没有解决方案。

因为没有 ENTRY_MODIFY 在复制结束时触发,而只是在复制开始时触发,所以我无法理解复制何时完成。您认为这可能是什么原因造成的?能修好吗,我怎么知道复制完成了?我无法更改 rhel 7.2,但除此之外我很乐意接受。提前致谢。

最佳答案

我只是检查文件长度是否为零。这里有一个例子

for (WatchEvent<?> event : key.pollEvents()) {
Path fileName = (Path) event.context();
if("tomcat-users.xml".equals(fileName.toString())) {
Path tomcatUsersXml = tomcatConf.resolve(fileName);
if(tomcatUsersXml.toFile().length() > 0) {
load(tomcatUsersXml);
}
}
}

关于java - WatchService 有时会触发 ENTRY_MODIFY 两次,有时会触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39147735/

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