void processEvents() { for (;;) {
makes me wonder: that looks very much like very suitable for the vert.x event queue (?). Where would I put such code? In the main loop, into a worker? Is there an example of WatchService used in vert.x?
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/5kOaEZxsWjk/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/5kOaEZxsWjk/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to vertx+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
@Override
public void start() {
try {
// Set up the watcher
watcher = FileSystems.getDefault().newWatchService();
logger.trace(logPrefix + "register all watchdirs");
for (Object dir : watchDirs) {
Path dirPath = Paths.get(System.getProperty("user.dir") + File.separator + dir);
WatchKey dirKey = dirPath.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
if (logger.isDebugEnabled()) {
logger.debug(logPrefix + "Watcher registered for: " + dirPath.toString());
}
watchKeys.put(dirKey, dirPath);
}
// periodically check for new events:
vertx.setPeriodic(500, new Handler<Long>() {
@Override
public void handle(Long timerId) {
try {
processEvents();
}
catch (IOException exc) {
logger.error(logPrefix + "I/O error occured while processing events: " + exc.getLocalizedMessage() + "(cause: " + exc.getCause() + ")");
}
catch (Exception exc) {
logger.error(logPrefix + "Unexpected error occured while processing events: " + exc.getLocalizedMessage());
}
}
});
}
catch (ClosedWatchServiceException exc) {
logger.warn(logPrefix + "The service was closed, exiting...");
}
}
private void processEvents() throws IOException, Exception {
for (;;) {
WatchKey key;
key = watcher.poll();
// watch key is null if no queued key is available (within the specified timeframe if a timeout was specified on the poll() request)
if (key == null) break;
logger.info(logPrefix + "Events received, start processing... (key: " + key + ")");
Path dir = watchKeys.get(key);
if (dir == null) {
logger.warn(logPrefix + "watchKey not recognized! (" + key.toString());
continue;
}
for (WatchEvent<?> event: key.pollEvents()) {
logger.info(logPrefix + "processing changes for: " + dir.toString());
WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
WatchEvent.Kind<Path> kind = watchEvent.kind();
if (kind.name().equals(StandardWatchEventKinds.OVERFLOW.name())) {
continue;
}
//The filename is the context of the event.
Path filename = watchEvent.context();
JsonObject watchMsg = new JsonObject();
// Add filename to message
watchMsg.putString("filename", filename.toString());
// Add file location (dir) to message
watchMsg.putString("location", dir.toString());
// Add type of event (create, modify, delete)
watchMsg.putString("type", kind);
// publish on eventbus
vertx.eventBus().publish("app.filechanges, watchMsg);
}
//Reset the key -- this step is critical if you want to receive
//further watch events. If the key is no longer valid, the directory
//is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid) {
logger.warn(logPrefix + "watchKey invalidated, removing from the list (" + key + ")");
watchKeys.remove(key);
// Exit if no keys remain
if (watchKeys.isEmpty()) {
break;
}
}
}
}
To unsubscribe from this group and all of its topics, send an email to vertx+un...@googlegroups.com.