JAVA 7手表服务 [英] JAVA 7 watch service

查看:163
本文介绍了JAVA 7手表服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何让监视服务处理应用程序启动时目录中的所有文件?

How can I have the watch service process any files that are in the directory on when the application starts up?

我已经在运行该应用程序,但是我注意到只处理了放置在目录中的新文件,而忽略了从一开始就存在的文件.

I already have the application running, but I noticed that only new files that are dropped in the directory are processed but files that were there from the start are ignored.

推荐答案

我在这里有相同的用例,但我感到惊讶的是,对于这种常见情况,我没有在网上找到很多有用的东西.我发现以下方法存在一些问题.假设我们利用walkTree方法扫描目录中的现有文件,然后为WatchService注册目录.

I have the same use case here and I am surprised that I did not find much useful online for such common scenario. I see some problems on the below approach. Let's say we utilize the walkTree method to scan the existing files in the directory and then register the directory for WatchService.

1. Files.walkTree(...);
2. Path dir =  Paths.get(...);
3. WatchService watcher = dir.getFileSystem().newWatchService();       
4. dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
5. // other logic

在第1行之后刚好在第5行开始之前创建的文件呢?我只是以此为界,使讨论变得容易.丢失文件机会的窗口的真实边界可能更大.

What about the files that are created after line 1 just finishes and before line 5 starts. I am just use this as a rough boundary to make discussions easier. The real boundary of window for opportunity to loss of files may be even broader.

这篇关于JAVA 7手表服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆