Java WatchService引发ClosedWatchServiceException
我正在使用java.nio.file.WatchService查看Spring引导应用程序中的目录。我一直收到以下异常,并且似乎未检测到监视文件夹中的任何文件更改:Java WatchService引发ClosedWatchServiceException,java,spring,spring-boot,watchservice,Java,Spring,Spring Boot,Watchservice,我正在使用java.nio.file.WatchService查看Spring引导应用程序中的目录。我一直收到以下异常,并且似乎未检测到监视文件夹中的任何文件更改: java.nio.file.ClosedWatchServiceException: null at sun.nio.fs.AbstractWatchService.checkOpen(AbstractWatchService.java:80) at sun.nio.fs.AbstractWatchService.take(Abst
java.nio.file.ClosedWatchServiceException: null
at sun.nio.fs.AbstractWatchService.checkOpen(AbstractWatchService.java:80)
at sun.nio.fs.AbstractWatchService.take(AbstractWatchService.java:117)
at com.company.MyFilesMonitor.monitorMappingFiles(MyMappingFilesMonitor.java:27)
at com.company.MyFilesMonitor$$FastClassBySpringCGLIB$$3d959079.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:752)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
这是使用watchService bean的类:
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.nio.file.*;
@Service
public class MyMappingFilesMonitor {
private final WatchService watchService;
private static final Logger LOGGER = LoggerFactory.getLogger(MyMappingFilesMonitor.class);
@Autowired
public MyMappingFilesMonitor(WatchService watchService) {
this.watchService = watchService;
}
@Async
@Scheduled(fixedDelay = 1000)
public void monitorMappingFiles() {
final WatchKey key;
try {
key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
final WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
@SuppressWarnings("unchecked")
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) event;
final Path filename = watchEventPath.context();
System.out.println("******** kind: " + kind + " -> " + filename);
key.reset();
}
} catch (InterruptedException e) {
return;
}
}
}
import org.slf4j.Logger;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.scheduling.annotation.Async;
导入org.springframework.scheduling.annotation.Scheduled;
导入org.springframework.stereotype.Service;
导入java.nio.file.*;
@服务
公共类MyMappingFileMonitor{
私人最终值班服务;
私有静态最终记录器Logger=LoggerFactory.getLogger(MyMappingFilesMonitor.class);
@自动连线
公共MyMappingFilesMonitor(WatchService WatchService){
this.watchService=watchService;
}
@异步的
@计划(固定延迟=1000)
public void monitorMappingFiles(){
最终观察键;
试一试{
key=watchService.take();
for(WatchEvent事件:key.pollEvents()){
final WatchEvent.Kind Kind=event.Kind();
if(kind==StandardWatchEventKinds.OVERFLOW){
继续;
}
@抑制警告(“未选中”)
最终WatchEvent watchEventPath=(WatchEvent)事件;
最终路径文件名=watchEventPath.context();
System.out.println(“******种类:“+种类+”->“+文件名);
键。重置();
}
}捕捉(中断异常e){
返回;
}
}
}
我不知道我错过了什么。我在这里和那里查看了不同的示例代码,我没有做任何不同的事情,但是我仍然一直得到这个异常。非常感谢您提供的任何帮助或指针。您正在使用try with resources创建新的监视服务,因此在方法
mappingfilesweach()返回该服务时,该服务始终处于关闭状态。
try(WatchService watchService = FileSystems.getDefault().newWatchService()) {
...
return watchService; // THIS IS AUTO-CLOSED
}
您只需删除try块。谢谢@DuncG。我将尝试它并更新结果。
try(WatchService watchService = FileSystems.getDefault().newWatchService()) {
...
return watchService; // THIS IS AUTO-CLOSED
}