英文:
Java NIO WatchService
问题
我有一个Java项目,我们需要保持监听某个路径,以便检测是否有任何新的XML文件,如果是真的,我们需要通过几个其他规则来处理它。
WatchService非常适合发现是否有新文件需要处理,但是我无法管理它,以便能够读取文件,它只会将文件名返回给我,从事件中。
是否有办法在WatchService中实现这一点?如果没有,还有其他建议吗?
谢谢。
英文:
I have Java project that we need to keep listening some path to detect if there is any new XML file and if true, we need to process it through few other rules.
The WatchService is doing very well to discover if I have a new file there to be processed, but I wasnt able to manage it so I could read the file, I just gives me back the file name, from the event.
Is there any way to that with WatchService? If not, whats another sugestion to reach?
Thanks.
答案1
得分: 1
当然,你可以轻松地实现这个:
- 创建监视服务:
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Path path = get(pathToSee);
path.register(watchService, new WatchEvent.Kind[]{ENTRY_MODIFY, ENTRY_CREATE}, SensitivityWatchEventModifier.HIGH);
watchEvent(watchService, path);
log.info("监视服务已创建!");
} catch (IOException e) {
log.error("在尝试创建监视服务时抛出异常:", e);
}
注意:如果有大量文件需要添加,你可以使用:
SensitivityWatchEventModifier.HIGH
来增加敏感度。
- 监视目录是否有变化:
WatchKey key;
while (true) {
try {
if ((key = watchService.take()) == null) break;
for (WatchEvent<?> event : key.pollEvents()) {
log.info("事件类型:" + event.kind()
+ "。受影响的文件:" + event.context() + "。");
String fileName = event.context().toString();
File directory = path.toFile();
yourService.readContent(directory, fileName);
}
key.reset();
} catch (InterruptedException | IOException e) {
log.error("尝试执行 watchEvent() 时出现中断异常:" + e);
}
}
- 最后,你可以根据这些信息执行你想要的操作:
try (BufferedReader br = new BufferedReader(new FileReader(directory + "/" + fileName))) {
String strLine;
while ((strLine = br.readLine()) != null) {
// 在这里执行你想要的操作
}
}
提示:
-
你可以创建一个单独的线程来执行这个任务,或者使用 Spring 的 @Async 来创建一个单独的线程来处理这些信息,以增加应用程序的并发性。
-
你也可以使用 Apache Commons 来实现这个功能!
例如:
public void getAll() throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(pathToSee);
observer.addListener(new FileAlterationListenerAdaptor() {
@SneakyThrows
@Override
public void onFileCreate(File file) {
// 在这里执行文件创建时的操作
}
@Override
public void onFileDelete(File file) {
// 在这里执行文件删除时的操作
}
});
}
英文:
Sure, you can easely to that:
-
Create the watch service:
WatchService watchService = null; try { watchService = FileSystems.getDefault().newWatchService(); Path path = get(pathToSee); path.register(watchService, new WatchEvent.Kind[]{ENTRY_MODIFY, ENTRY_CREATE}, SensitivityWatchEventModifier.HIGH); watchEvent(watchService, path); log.info("Watch Service has ben created!"); } catch (IOException e) { log.error("Exception has ben throw when the service have tried to createWatchService()", e); }
Note: If you have a large amount of files to be added, you can put:
SensitivityWatchEventModifier.HIGH
To increase sensitivity.
-
Watch if have changes in you directory:
WatchKey key; while (true) { try { if ((key = watchService.take()) == null) break; for (WatchEvent<?> event : key.pollEvents()) { log.info("Event kind:" + event.kind() + ". File affected: " + event.context() + "."); String fileName = event.context().toString(); File directory = path.toFile(); yourService.readContent(directory, fileName); } key.reset(); } catch (InterruptedException | IOException e) { log.error("InterruptedException when try watchEvent()" + e); } }
-
And finally, you can do what you want to do with that infos:
try (BufferedReader br = new BufferedReader(new FileReader(directory + "/" + fileName))) { String strLine; while ((strLine = br.readLine()) != null) { } } } }
Tips:
-
You can create a separete thread to do that, or use Spring @Async to create a separete thread to process this information and increase concurrency in your application.
-
You can use Apache Commons you to do that too!
e.g:
public void getAll() throws Exception {
FileAlterationObserver observer = new FileAlterationObserver(pathToSee);
observer.addListener(new FileAlterationListenerAdaptor() {
@SneakyThrows
@Override
public void onFileCreate(File file) {
}
@Override
public void onFileDelete(File file) {
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论