WatchService race condition?

E

Eric Sosman

I had occasion recently to use java.nio.file.WatchService
for the first time, and have run across a problem I hope more
experienced practitioners can solve. How do I iterate over the
current contents of a directory and then get notified of new
arrivals? If I do

iterate_over_the_files();
register_with_watchservice();

.... then there's a race: Any file created after iteration (or
after the directory gets read) but before registration gets
missed. But if I do

register_with_watchservice();
iterate_over_the_files();

.... then there's a different race: Any file created after
registration but before the iteration slurps the directory
is processed twice.

I wound up keeping a Set of the files already processed,
and using the WatchService only as a "This would be a good
time to run another iteration" notice. But that's clumsy: It
feels like there ought to be a sleeker way. Any ideas?

(Application: One program periodically fetches data from a
URL and dumps it into a time-stamped file in a known directory.
Another program reads the time-stamped files and extracts data
into a JFreeChart, which it would like to update whenever the
first program deposits a new file. It's working, but ... ick.)
 
A

Arne Vajhøj

I had occasion recently to use java.nio.file.WatchService
for the first time, and have run across a problem I hope more
experienced practitioners can solve. How do I iterate over the
current contents of a directory and then get notified of new
arrivals? If I do

iterate_over_the_files();
register_with_watchservice();

... then there's a race: Any file created after iteration (or
after the directory gets read) but before registration gets
missed. But if I do

register_with_watchservice();
iterate_over_the_files();

... then there's a different race: Any file created after
registration but before the iteration slurps the directory
is processed twice.

I wound up keeping a Set of the files already processed,
and using the WatchService only as a "This would be a good
time to run another iteration" notice. But that's clumsy: It
feels like there ought to be a sleeker way. Any ideas?

(Application: One program periodically fetches data from a
URL and dumps it into a time-stamped file in a known directory.
Another program reads the time-stamped files and extracts data
into a JFreeChart, which it would like to update whenever the
first program deposits a new file. It's working, but ... ick.)

Either do as you do now or use a between processes locking
mechanism (like FileChannel lock).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,044
Members
48,769
Latest member
Clifft

Latest Threads

Top