Note that there are some explanatory texts on larger screens.

plurals
  1. POJava 7 WatchService - Ignoring multiple occurrences of the same event
    primarykey
    data
    text
    <p>The javadoc for <code>StandardWatchEventKinds.ENTRY_MODIFY</code> says:</p> <blockquote> <p>Directory entry modified. When a directory is registered for this event then the WatchKey is queued when it is observed that an entry in the directory has been modified. The event count for this event is 1 or greater.</p> </blockquote> <p>When you edit the content of a file through an editor, it'll modify both date (or other metadata) and content. You therefore get two <code>ENTRY_MODIFY</code> events, but each will have a <code>count</code> of 1 (at least that's what I'm seeing).</p> <p>I'm trying to monitor a configuration file (<code>servers.cfg</code> previously registered with the <code>WatchService</code>) that is manually updated (ie. through command line <code>vi</code>) with the following code:</p> <pre><code>while(true) { watchKey = watchService.take(); // blocks for (WatchEvent&lt;?&gt; event : watchKey.pollEvents()) { WatchEvent&lt;Path&gt; watchEvent = (WatchEvent&lt;Path&gt;) event; WatchEvent.Kind&lt;Path&gt; kind = watchEvent.kind(); System.out.println(watchEvent.context() + ", count: "+ watchEvent.count() + ", event: "+ watchEvent.kind()); // prints (loop on the while twice) // servers.cfg, count: 1, event: ENTRY_MODIFY // servers.cfg, count: 1, event: ENTRY_MODIFY switch(kind.name()) { case "ENTRY_MODIFY": handleModify(watchEvent.context()); // reload configuration class break; case "ENTRY_DELETE": handleDelete(watchEvent.context()); // do something else break; } } watchKey.reset(); } </code></pre> <p>Since you get two <code>ENTRY_MODIFY</code> events, the above would reload the configuration twice when only once is needed. Is there any way to ignore all but one of these, assuming there could be more than one such event? </p> <p>If the <code>WatchService</code> API has such a utility so much the better. (I kind of don't want to check times between each event. All the handler methods in my code are synchronous.</p> <p>The same thing occurs if you create (copy/paste) a file from one directory to the watched directory. How can you combine both of those into one event?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload