如何检查apache commons中的传入文件传输状态io monitor

时间:2016-04-05 14:03:09

标签: java apache apache-commons

我已经使用Apache commons io monitor实现了一个简单的文件监听器。我还实现了一个selenium脚本,它只是将文件下载到预硬编码路径文件夹中。这完全正常。我的监听器监视下载的文件和收集必要的信息。根据我的要求,一旦我的selenium脚本完成执行,我应该能够停止文件监听器。为此,我必须知道传入文件传输状态以更好地处理它因为我无法阻止监听器中间的文件传输。(有时下载文件可能很重,所以下载需要一些时间)。所以我怎么能知道Apache commons中的传入文件状态io监视器在停止文件监听器之前。如果有人知道,请告诉我。

示例代码段

    public class SimpleTestMonitor {
    // A hardcoded path to a folder you are monitoring .
    public static final String FOLDER =
            "/home/skywalker/Desktop/simple-test-monitor/watchdir";

    public static void main(String[] args) throws Exception {
        // The monitor will perform polling on the folder every 5 seconds
        final long pollingInterval = 5 * 1000;

        File folder = new File(FOLDER);

        if (!folder.exists()) {
            // Test to see if monitored folder exists
            throw new RuntimeException("Directory not found: " + FOLDER);
        }

        FileAlterationObserver observer = new FileAlterationObserver(folder);
        FileAlterationMonitor monitor =
                new FileAlterationMonitor(pollingInterval);
        FileAlterationListener listener = new FileAlterationListenerAdaptor() {
            // Is triggered when a file is created in the monitored folder
            @Override
            public void onFileCreate(File file) {
                try {
                    // "file" is the reference to the newly created file
                    System.out.println("File created: "
                            + file.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }

            // Is triggered when a file is deleted from the monitored folder
            @Override
            public void onFileDelete(File file) {
                try {
                    // "file" is the reference to the removed file
                    System.out.println("File removed: "
                            + file.getCanonicalPath());
                    // "file" does not exists anymore in the location
                    System.out.println("File still exists in location: "
                            + file.exists());
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
            }
        };

        observer.addListener(listener);
        monitor.addObserver(observer);
        monitor.start();
    }
}

Reference

0 个答案:

没有答案