文件夹扫描服务的设计
我们有一个旧的 java 应用程序包装为服务,本质上是一个 while 循环,它迭代文件夹以查找正在创建的新文件。
除了与大文件有点不一致之外,它运行良好,但让我想知道这是否仍然是一个非常基本的方法,或者它是否是“查找更新”应用程序的唯一方法
We have an old java app wrapped as a service that essentially is a while loop, which iterates through folders looking for new files being created.
Apart of being a little inconsistent with big files, it works well, but makes me wonder if this is still a very basic approach or if it is the only approach when it comes to "look for updates" apps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Java 7,则可以使用 WatchService API。 这篇文章提供了很好的介绍。
如果操作系统支持,WatchService 比轮询更有效;如果操作系统不支持任何更高级的方式来监视文件系统的更改,它会自动回退到轮询。
If you are using Java 7, you can use the WatchService API. This article provides a good introduction.
WatchService is more efficient than polling if the OS supports it, and it will automatically fall back to polling if the OS does not support any more advanced way of watching the file system for changes.
如果您不必坚持使用 Java,您可以尝试在服务中使用 .NET 的
FileSystemWatcher
类,而不是您的 java 实现。If you don't have to stick with Java, you might try .NET's
FileSystemWatcher
class inside of the service instead of your java implementation.