Java WatchService代码在Eclipse中工作,但在Docker中的Tomcat中同样的代码失败
在Eclipse中,单元测试触发此代码,当被监视的文件被更改时,输出新文本 在Tomcat中,它挂在显示的位置 我已经在网上趴了很长一段时间,试图找到有类似问题的人,但什么也没发现 这是一个普通的watcher代码,因为它在Eclipse中工作得很好,所以一定有一个Tomcat特定的问题,但是什么呢 Tomcat中的代码可以访问该文件。这不是权限问题Java WatchService代码在Eclipse中工作,但在Docker中的Tomcat中同样的代码失败,eclipse,tomcat,watchservice,Eclipse,Tomcat,Watchservice,在Eclipse中,单元测试触发此代码,当被监视的文件被更改时,输出新文本 在Tomcat中,它挂在显示的位置 我已经在网上趴了很长一段时间,试图找到有类似问题的人,但什么也没发现 这是一个普通的watcher代码,因为它在Eclipse中工作得很好,所以一定有一个Tomcat特定的问题,但是什么呢 Tomcat中的代码可以访问该文件。这不是权限问题 public class Foo extends Thread { File watchedFile = <the watched
public class Foo extends Thread {
File watchedFile = <the watched file>;
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = watchedFile.getParentFile().toPath();
dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
@Override
public void run() {
while (true) {
WatchKey key;
try {
key = watcher().take(); <<< HANGS HERE IN TOMCAT, DOESN'T HANG HERE IN ECLIPSE.
} catch (InterruptedException | ClosedWatchServiceException e) {
break;
}
try {
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
WatchEvent<Path> pathEvent = (WatchEvent<Path>)event;
if (pathEvent.context().toString().equals(watchedFile.getName()) {
// Do something.
}
}
}
} catch (Exception e) {
throw new RuntimeException("Trouble: " + e.getMessage(), e);
}
if (!key.reset()) {
break;
}
}
}
}
公共类Foo扩展线程{
文件watchedFile=;
WatchService watcher=FileSystems.getDefault().newWatchService();
Path dir=watchedFile.getParentFile().toPath();
目录寄存器(watcher、StandardWatchEventTypes.ENTRY\u MODIFY);
@凌驾
公开募捐{
while(true){
监视键;
试一试{
key=watcher().take();