我试图用spring执行"WatchService“,但这看起来是不可能的,因为当我试图在应用程序上下文中运行此服务时,但是当控制到达时,加载spring上下文就会停止。
key = watcher.take();由于这种加载的应用程序上下文没有发生。
下面是完整的代码
@Component
public class DirectoryWatchDemo {
@PostConstruct
public static void test(){
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get("C:/test");
dir.register(watcher, ENTRY_CREATE);
System.out.println("Watch Service registered for dir: " + dir.getFileName());
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY &&
fileName.toString().equals("DirectoryWatchDemo.java")) {
System.out.println("My source file has changed!!!");
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}我这样做是因为我不想手动执行"WatchService“。
发布于 2016-09-06 15:04:33
这是因为您有一个没完没了的循环,因此@PostConstruct的方法调用永远不会返回。
(我觉得奇怪的是,@PostConstruct与静态方法一起工作,但也许这是可行的)
因此,解决办法是为观察者启动一个新线程。你可以用不同的方式做这件事:
@Async添加到该方法(我不知道这是否适用于@PostConstruct方法)(主要缺点是,这是在初始化整个应用程序上下文之前启动的)@Scheduler注释:@Scheduled(fixedDelay = Long.MAX_VALUE) -(与@PostConstruct+@Async相比,它的优点是:它确实工作,并且在初始化完完整的上下文之后就开始了)发布于 2016-09-06 15:04:21
WatchService.take等待下一个手表键:“检索并移除下一个手表键,如果还没有出现,则等待。”
PostConstruct --它是Java注释,而不是Spring注释--“用于在执行任何初始化时都需要执行的方法。必须在类投入服务之前调用该方法。”基于这些文档,似乎PostConstruct必须返回才能将bean放入服务中(“必须在类被投入服务之前调用”)。
但是您的PostConstruct方法不返回;因此,PostConstruct不是您所需要的。
您可以考虑实现SpringInitializingBean接口,它提供了一个回调方法afterPropertiesSet。它应该允许您启动这种类型的服务方法。
否则,您可以查看Apache VFS2虚拟文件系统,它包括一个文件夹监视程序。这就是我的项目所使用的;在系统启动时启动监视程序非常容易;而且,它还监视文件删除、更新和创建事件(与Camel文件观察者不同)。
https://stackoverflow.com/questions/39351863
复制相似问题