JAVA 7 观看服务

发布于 2025-01-08 08:39:58 字数 92 浏览 4 评论 0原文

如何让手表服务在应用程序启动时处理目录中的任何文件?

我已经运行了该应用程序,但我注意到只有放入目录中的新文件才会被处理,而从一开始就存在的文件将被忽略。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执妄 2025-01-15 08:39:58

WatchService 仅处理文件系统中的更改。已经存在的文件尚未更改,因此 WatchService 不会获取这些文件。
您必须递归地遍历所有文件和目录才能获得文件的初始“视图”:

Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file,
                    BasicFileAttributes attrs) throws IOException {
                // do something with the file
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir,
                    BasicFileAttributes attrs) throws IOException {
                // do something with the directory
                return FileVisitResult.CONTINUE;
            }
        });

初始化后发生的所有更改都会由 WatchService 获取。

The WatchService does only cope with changes in the filesystem. The files that are already there have not been changed and thus aren't picked up by the WatchService.
You would have to traverse all files and directory recursivly to get an initial 'view' of the files:

Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file,
                    BasicFileAttributes attrs) throws IOException {
                // do something with the file
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir,
                    BasicFileAttributes attrs) throws IOException {
                // do something with the directory
                return FileVisitResult.CONTINUE;
            }
        });

All changes, that occur after initializations are then picked up by the WatchService.

傾旎 2025-01-15 08:39:58

我在这里有相同的用例,令我惊讶的是我在网上没有发现对于这种常见场景有太多用处。我发现以下方法存在一些问题。假设我们使用 walkTree 方法扫描目录中的现有文件,然后为 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 行开始之前创建的文件呢?我只是用它作为一个粗略的界限,以使讨论更容易。文件丢失机会窗口的真正边界可能更宽。

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

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.

生生漫 2025-01-15 08:39:58

WatchService 监视已注册对象的某些类型的更改和事件。当我们正在监听的事件发生时,代码就会被调用。我们可以监视文件的创建、删除或修改:

  • ENTRY_CREATE
  • ENTRY_DELETE
  • ENTRY_MODIFY

如果使用

WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

...,则仅报告新创建的文件。为了观察已创建的文件,请使用:

StandardWatchEventKinds.ENTRY_MODIFY

WatchService watches registered objects for certain types of changes and events. Code gets called when event, we are listening to, occurs. We may monitor either creation, deletion or modification of the file(s):

  • ENTRY_CREATE
  • ENTRY_DELETE
  • ENTRY_MODIFY

If using

WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);

… only newly created files will be reported. In order to observe already created files, use:

StandardWatchEventKinds.ENTRY_MODIFY
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文