我一直在从事以下项目,一些背景:
我是一名实习生,目前正在为我的组织开发新的搜索系统。当前的设置是 microsoft sharepoint 2013,其中用户上传文件等。另一方面是我正在开发的系统,它将所有上传到 apache SOLR 的数据建立索引。
我已经成功地将共享点内容存储库映射到网络驱动器,并且我可以手动启动我的程序,以使用 Solrj api 开始将该网络驱动器的内容索引到 SOLR。
但是我面临的问题是我无法从该网络驱动器轮询事件。在本地运行的测试版本中,我使用观察程序服务来启动文件创建、文件修改和文件删除的代码(重新索引文档、删除索引)。
不幸的是,这对于指向网络驱动器的 url 不起作用:(。
所以最大的问题是:是否有任何 API/库可用于从网络驱动器轮询事件?
任何帮助将不胜感激!
最佳答案
所以我终于弄清楚了这个问题,尝试查看.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();
}
}
关于windows - 从网络目录轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32310066/