从网络目录轮询

时间:2015-08-31 11:22:31

标签: windows sharepoint networking solr polling

我一直致力于以下项目,一些背景知识:

我是一名实习生,正在为我的组织开发一个新的搜索系统。当前的设置是microsoft sharepoint 2013,其中用户上传文件等。另一方面是我正在开发的系统,它将所有数据上传到apache SOLR。

我已成功将sharepoint内容存储库映射到网络驱动器,我可以手动启动我的程序,开始使用Solrj api将此网络驱动器的内容索引到SOLR。

然而,我面临的问题是我无法从此网络驱动器轮询事件。在我运行本地的测试版本中,我使用了一个观察者服务来启动文件创建,文件修改和文件删除的代码(reindex文档,删除索引)。

这对于指向网络驱动器的网址不合适:(。

所以最大的问题是:是否有可用于从网络驱动器轮询事件的API /库?

任何帮助都会受到极大的赞赏!

1 个答案:

答案 0 :(得分:2)

所以我最终想出了这个,尝试查看.net的观察者服务变体(system.io.filesystemwatcher),我遇到了同样的问题。我终于通过使用java.io.FileAlterationMonitor / observer来实现它。

代码:

public class UNCWatcher {
// A hardcoded path to a folder you are monitoring .
public static final String FOLDER =
        "A:\\Department";

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());



                if(file.getName().endsWith(".docx")){
                    System.out.println("Uploaded resource is of type docx, preparing solr for indexing.");
                }


            } 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);
    System.out.println("Starting monitor service");
    monitor.start();
  }
}