Comments (3)
Thanks for the report @J-N-K. I'm guessing that's actually because when you call watch()
it still needs to traverse the directory to get initial hashes of all the files before it can start watching, and there's no way to know when it finishes the initialization phase and starts the actual listening phase.
You may be able to use the watchAsync
method instead, which actually does the initialization synchronously and returns a future as soon as the watcher starts watching. I think something like this would be roughly equivalent to the code you have:
dirWatcher = DirectoryWatcher.builder().listener(this).path(basePath).build();
executor = Executors.newSingleThreadExecutor();
dirWatcher.watchAsync(executor);
then you should be able to shut it down with:
dirWatcher.close();
executor.shutdown();
Of course that has the downside of having to block during the initialization phase, but you could wrap that in another future perhaps and provide some async method of seeing when it starts watching. You might also consider disabling hashing, which makes this initialization phase much faster, though it increases the likelihood of duplicate events.
Nevertheless I think it's an issue that watch()
provides no way of knowing when the initialization phase ends and the actual watching starts. We'd have to think about the best way to communicate this. Perhaps it'd make sense to provide a signal to the listener in that case. Let me know if you have any ideas on the best approach here.
from directory-watcher.
I have refactored a bit to use .watchAsync
(which was not so easy, because we can't block in service activation) and it seems to avoid these issues. Unfortunately I don't have good idea how to signal that.
from directory-watcher.
I can't think of any way around this problem if you want hashing. I guess we could start watching while we're doing the initial hashing of the files, then just emit the events as-is if we haven't hashed the file yet. Does your use case require the hashing functionality?
from directory-watcher.
Related Issues (20)
- [JAVA] 0.9.3 Copy & Remove of folder structure does not produce CREATE events HOT 1
- Missing tag `0.9.6` upstream HOT 1
- New Release HOT 4
- Absolute path prevents events on macOS HOT 3
- Question on Using 2 Classes HOT 1
- LibCarbon failure on MacOS Big Sur HOT 12
- Allow watching directory non-recursively HOT 1
- Unable to detect file deletion events HOT 27
- File creation on Windows is sometimes not detected for copied files HOT 3
- Ensure consistent behavior when a root watched directory is deleted
- Define a minimal public API
- Watcher does not detect (re)moving a parent directory HOT 2
- DirectoryWatcher.watch() throws ClosedWatchServiceException when Watcher is closed HOT 4
- Access denied stops walking file tree HOT 7
- Does it work with mounted volumes with Docker? HOT 4
- Modification events are lost depending on editor - how the file is written - neovim HOT 7
- Duplicate events when editing file on Samba share HOT 7
- Fails with NoClassDefFoundError inside Wildfly HOT 2
- Suggestion: Fail (or handle!) watched path being a file instead of a directory
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from directory-watcher.