3

I am using WatchService of NIO. Two classes to watch a specific directory (Ubuntu 11.10).

Whenever a change is made, for example if I modify a existing file, it fires a MODIFIED event 2 times. Don't know why? It works fine when I create new file.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54

3 Answers3

6

I found a solution for it. It fires two modify events, because when the filesystem writes new files, it creates it with zero byte and fires create event and then it writes data to the and file then it fires the modify event.

The same applies with modifications. When we modify an already existing file, the filesystem first creates it with 0 bytes and fires a modify event and then writes data to it. Then it fires the modify event again. That's why I was receiving two modify events.

Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54
1

I've used example from Oracle, and strangely sometimes it fires ENTRY_MODIFY twice and sometimes it fires it once. I am using Windows machine.

In the docs there is no info about possible duplicated events, so I guess it is either an undocumented behavior or a bug.

  • 1
    Thanks For reply, I got solution. it's not because of bug. when we write or edit file. OS FileSystem create a file first with 0 byte then it make changes in it. that's why we receive two events on modification. similarly when we create a new file(with some data or paste some file in that derictory), it fires two events first for create and second for modification. – Muneeb Nasir Sep 10 '12 at 09:40
  • Consider following scenario: 1) I open file for modification. 2) I change it and save. 3) Sometimes I have two events, sometimes - only one. Why it behaves differently then? – Sergey Nikitin Sep 10 '12 at 10:12
  • I got 2 events every time when i modify file. As per my knowledge it should be notified 2 time when you modify your file. Don't know why you are notified once - mat be because of OS difference. – Muneeb Nasir Sep 10 '12 at 10:50
  • are you getting modify event once on file creation time? – Muneeb Nasir Sep 12 '12 at 08:57
  • Yes, I get it only once for file creation – Sergey Nikitin Sep 12 '12 at 10:08
  • then there is noting wrong with you code. its fine. I already explained it above. going to repeat again, when you create new file, it will first initiated with zero byte and fire create event and then modified by OS and it fire modify event. – Muneeb Nasir Oct 19 '12 at 08:06
1

The Oracle documentation says that "For example, when a file in a watched directory is modified then it may result in a single ENTRY_MODIFY event in some implementations but several events in other implementations" source: https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html

In my case WatchService reported correctly one ENTRY_MODIFY per one file modification in different implementations (for example in standalone java programs and in a web application project in Eclipse ) and different environments (laptop vs server) but when I deployed my web application on a Tomcat server always two ENTRY_MODIFY events per one file modification occurred. The reason for this difference was that I had multiple Main classes in my Maven web application which all were started when the web application was deployed on my tomcat server, despite I defined start.class in the pom.xml file:

<start-class>com. ... .Application</start-class>  

In Eclipse only the start-class started. It seems that different file structure in the WAR file compared to the Eclipse Maven project structure causes this difference. My solution was to delete redundant main classes and files and then there was only one ENTRY_MODIFY per one file modification.