4

Reading the javadocs for WatchEvent, I see that the count() method lets me know if an event is repeated by its result.

Returns the event count. If the event count is greater than 1 then this is a repeated event.

What does that mean, exactly? Does it mean that two or more WatchEvent objects refer to the same "event" (e.g. a file being created)?

I am experimenting with the example on Oracle's site for the new WatchService API and this part confused me, especially because I will get a different number of events for successive runs of the same test code (in which I write to a file using a FileWriter without interacting with it manually), but the result of count() is never more than 1.

Edit: I realize that this may be related to this other question about repeated events, but that doesn't answer what it actually means for an event to be repeated.

Community
  • 1
  • 1
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
  • Could it mean something like a double-click? – Dawood ibn Kareem Feb 24 '14 at 20:49
  • @DavidWallace I don't know, you tell me. I'm not even clicking on the file under question, so I can't tell. – 2rs2ts Feb 24 '14 at 20:51
  • I've never used this class--but if you're watching for files created in a directory, and then a file is created in the directory, and then later another file is created in the directory, maybe that's a repeated event? – ajb Feb 24 '14 at 21:15
  • @ajb I certainly hope it isn't, because then what's the point in checking if the `count` is greater than 1? I'd have to check the event context every time just to be sure. – 2rs2ts Feb 24 '14 at 21:30

2 Answers2

2

I spent a while experimenting with this and this is my conclusion:

An event is repeated, for file f and event type e, if multiple e type events occurred to the same file f.

If an e type event occurred to multiple files f1, f2, and f3, but only once each, then none of those events are repeated.

When you .take() a WatchKey from the WatchService, you will get a WatchEvent for each file which experienced an event, for each event type it experienced. The .count() will be greater than 1 if that type of event happened to that file multiple times since you last get a WatchKey from the WatchService.

That is, if you are watching a directory which has A.txt in it, and then the following events happen:

  1. B.txt is created in the directory.
  2. B.txt is written to.
  3. A.txt is written to.
  4. B.txt is deleted.
  5. A.txt is written to again.

You will get four WatchEvents:

  1. The create event for B.txt.
  2. The modify event for B.txt.
  3. The modify event for A.txt. Its .count() will be 2.
  4. The delete event for B.txt.

This means that you will never get more than 3*n events in your WatchKey, where n is the number of files that were in the directory at any point.

There are four types of events defined in StandardWatchEventKinds, but the OVERFLOW event indicates that events may have been lost, so I omitted it from the above number.

Community
  • 1
  • 1
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
  • If all you're doing is say reading the file contents when you get a modify event, doesn't that mean you can just ignore duplicate/repeated events? As the contents of the file, at the time you handle the event is what matters and it doesn't seem like you could get the content on the first of repeated event? – Toby Apr 06 '17 at 14:41
0

The watch service allows you to register a listener for events in directories. If an event happens more than once before your service is notified, it gets an increment, instead of having two such events causing notification.

This is specifically about files, so it handles file creation, modification, deletion in a directory.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Can you explain what you mean by "an event happens more than once?" Say I modified two files in the directory before my `WatchService` noticed, will I get two modification events from `key.pollEvents()` or will I get one modification event with a count of 2? – 2rs2ts Feb 24 '14 at 21:32
  • The way I read the documentation, you monitor a directory; if two files were modified before the WatchService got around to constructing the notification, you would get one notification that two events had happened. – arcy Feb 24 '14 at 21:48
  • What do you mean by that? I was only modifying one file so I modified my test code to modify two files, and I got two modify events. I added a `wait` of 1 second before taking the events from the `WatchService` and I got two modify events, each with a count of 2 (probably for the same reason as in the linked question.) Does this mean that the `count` refers to the number of times a particular type of event happened to a particular file in that directory? e.g. if file `A.txt` was written to several times, the modify event with the `context()` `A.txt` would have a `count` greater than 1? – 2rs2ts Feb 25 '14 at 13:26