file watcher - Continuously reading java WatchEvents -


i'm trying synchronize 2 folders , sub directories between client , server. have modified version of this class i've posted below. in client class, create watchdir object , call processevents() method in infinite loop. method returns mytuple object (a struct containing event type , path object) if event registered , null if not. problem seems work first event happen in directory (i.e. if add file watched folder, watchdir object.processevents() returns 1 tuple entry_create event , never returns tuple other file additions/deletions/modifications happen after). i'd processevents continuously called (hence infinite while) returning tuple each time event occurs. help!

my modified watchdir:

import static java.nio.file.standardwatcheventkinds.*; import static java.nio.file.linkoption.*; import java.nio.file.attribute.*; import java.io.*; import java.util.*; import java.util.concurrent.timeunit;  public class watchdir {     private final watchservice watcher;     private final map<watchkey,path> keys;     private final boolean recursive;     private boolean trace = false;      public watchdir(path dir, boolean recursive) throws ioexception {         this.watcher = filesystems.getdefault().newwatchservice();         this.keys = new hashmap<watchkey,path>(); //holds key each subdirectory         this.recursive = true;          registerall(dir);     }      public void registerall(path start) throws ioexception {         files.walkfiletree(start, new simplefilevisitor<path>() {             @override             public filevisitresult previsitdirectory(path dir, basicfileattributes attrs) throws ioexception {                 register(dir);                 return filevisitresult.continue;             }         });     }      public void register(path dir) throws ioexception {         watchkey key = dir.register(watcher, entry_create, entry_delete, entry_modify);         keys.put(key, dir);     }      public mytuple processevents() {         watchkey key;         //while (true) {              try {                 key = watcher.take();             } catch (interruptedexception e) {                 return new mytuple("interrupted", null);             }              path dir = keys.get(key); //get next subdirectory path             if (dir == null)                 return new mytuple("null directory", null);              (watchevent<?> event : key.pollevents()) {                 watchevent.kind kind = event.kind();                 watchevent<path> ev = cast(event);                 path name = ev.context();                 path child = dir.resolve(name);                  return new mytuple(event.kind().name(), child);             }             return null;         //}     }      @suppresswarnings("unchecked")     static <t> watchevent<t> cast(watchevent<?> event) {         return (watchevent<t>)event;     } } 

my client:

import java.nio.file.attribute.*; import java.nio.file.*; import java.util.concurrent.timeunit;  public class newclient {     public static void main(string[] args) throws ioexception {          path folder = paths.get(system.getproperty("user.dir"));         watchdir watcher = new watchdir(folder, true);         mytuple thistuple;      while (true) {         thistuple = watcher.processevents();         string event = thistuple.getevent();         path path = thistuple.getpath();          system.out.println(event+": "+path.tostring());     } } } 

you don't reset key. read docs again:

once events have been processed consumer invokes key's reset method reset key allows key signalled , re-queued further events.

probably here

       (watchevent<?> event : key.pollevents()) {             watchevent.kind kind = event.kind();             watchevent<path> ev = cast(event);             path name = ev.context();             path child = dir.resolve(name);              return new mytuple(event.kind().name(), child);         }         key.reset();         return null; 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -