Differences Between the Java WatchService API and the Apache Commons IO Monitor Library – Java WatchService API和Apache Commons IO监控库之间的区别

最后修改: 2016年 12月 24日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Long before the Java WatchService API was released in Java 7, Apache Commons IO Monitoring library was already addressing the same use-case of monitoring a file system location or directory for changes.

早在Java WatchService API在Java 7中发布之前,Apache Commons IO Monitoring库已经在处理相同的用例,即监测文件系统位置或目录的变化。

In this article, we are going to explore the differences between the two APIs.

在这篇文章中,我们将探讨这两个API之间的差异。

2. Maven Dependencies

2.Maven的依赖性

To use Apache Commons IO, the following dependency needs to be added in the pom:

为了使用Apache Commons IO,需要在pom中添加以下依赖。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

And of course the watch service is part of the JDK, so it needs no external dependency.

当然,观察服务是JDK的一部分,所以它不需要外部依赖。

3. Feature Comparison

3.功能比较

3.1. Event Driven Processing

3.1.事件驱动处理

WatchService API is driven by the file system change events triggered by the operating system. This approach saves the application from polling the file system repeatedly for changes.

WatchService API由操作系统触发的文件系统变化事件驱动。这种方法使应用程序无需反复轮询文件系统的变化。

Apache Commons IO Monitor library on the other hand, polls the file system location at a configurable sleep interval by calling the listFiles() method of File class. This approach wastes CPU cycles, especially if no change occurs.

另一方面,Apache Commons IO Monitor库通过调用File类的listFiles()方法,在可配置的睡眠间隔内轮询文件系统位置。这种方法浪费了CPU周期,尤其是在没有发生变化的情况下。

3.2. Callback Method

3.2.回调方法

WatchService API does not provide callback methods. Instead it provides two types of polling methods to check if new change events are available for processing:

WatchService API不提供回调方法。相反,它提供了两种类型的轮询方法来检查是否有新的变化事件可供处理。

  1. Blocking methods like poll() (with a timeout parameter) and take()
  2. Non-blocking method like poll() (without a timeout parameter)

With the blocking methods, the application thread starts processing only when new change events are available. Therefore, it needs not keep on polling for new events.

使用阻塞方法,应用线程只有在有新的变化事件时才开始处理。因此,它不需要不断地轮询新的事件。

The details and usage of these methods can be found in our article here.

这些方法的细节和用法可以在我们的文章中找到

In contrast, Apache Commons IO library provides callback methods on the FileAlterationListener interface which are invoked when a change in the file system location or directory is detected.

相比之下,Apache Commons IO库在FileAlterationListener接口上提供了回调方法,当检测到文件系统位置或目录发生变化时就会调用这些方法。

FileAlterationObserver observer = new FileAlterationObserver("pathToDir");
FileAlterationMonitor monitor = new FileAlterationMonitor(POLL_INTERVAL);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
    @Override
    public void onFileCreate(File file) {
        // code for processing creation event
    }

    @Override
    public void onFileDelete(File file) {
        // code for processing deletion event
    }

    @Override
    public void onFileChange(File file) {
        // code for processing change event
    }
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();

3.3. Event Overflow

3.3.事件溢出

WatchService API is driven by the operating system events. Hence, there is a possibility that the operating system buffer that holds the events overflows, if the application cannot process the events quickly enough. In this scenario, the event StandardWatchEventKinds.OVERFLOW is triggered indicating that some of the events are lost or discarded before the application could read them.

WatchService API是由操作系统事件驱动的。因此,如果应用程序不能足够快地处理这些事件,就有可能出现持有事件的操作系统缓冲区溢出的情况。在这种情况下,事件StandardWatchEventKinds.OVERFLOW被触发,表明一些事件在应用程序可以读取之前就被丢失或丢弃了。

This requires proper handling of the OVERFLOW event in the application to ensure that the application can handle any sudden burst of change events that may trigger the OVERFLOW event.

这需要在应用程序中正确处理OVERFLOW事件,以确保应用程序能够处理任何可能触发OVERFLOW事件的突然爆发的变化事件。

The Commons IO library on the other hand, is not based on the operating system events and hence there is no question of overflow.

另一方面,Commons IO库不以操作系统事件为基础,因此不存在溢出的问题。

In every poll, the observer gets the list of files in the directory and compares it with the list obtained from the previous poll.

在每次轮询中,观察者都会得到目录中的文件列表,并将其与前一次轮询得到的列表进行比较。

  1. If a new file name is found in the last poll, onFileCreate() is invoked on the listener
  2. If a file name found in the previous poll is missing in the file list obtained from the last poll, onFileDelete() is invoked on the listener
  3. If a match is found, the file is checked for any change in attributes like last modified date, length etc. If a change is detected, onFileChange() is invoked on the listener

4. Conclusion

4.结论

In this article we have managed to highlight the key differences in the two APIs.

在这篇文章中,我们设法强调了两个API的主要区别。

And, as always, the full source code for the examples used in this article is available in our GitHub project.

而且,像往常一样,本文中使用的例子的完整源代码可在我们的GitHub项目中获得。