use of java.nio.file.WatchEvent in project hive by apache.
the class DirWatcher method watch.
public void watch() {
while (!shutdown.get()) {
WatchKey watchKey;
try {
watchKey = watchService.take();
} catch (InterruptedException e) {
if (shutdown.get()) {
LOG.info("Shutting down watcher");
break;
} else {
LOG.error("Watcher interrupted before being shutdown");
throw new RuntimeException("Watcher interrupted before being shutdown", e);
}
}
Path watchedPath = (Path) watchKey.watchable();
WatchedPathInfo parentWatchedPathInfo = watchedPaths.get(watchedPath);
boolean cancelledWatch = false;
for (WatchEvent<?> rawEvent : watchKey.pollEvents()) {
if (rawEvent.kind().equals(OVERFLOW)) {
// Ignoring and continuing to watch for additional elements in the dir.
continue;
}
WatchEvent<Path> event = (WatchEvent<Path>) rawEvent;
WatchedPathInfo watchedPathInfo;
Path resolvedPath;
switch(parentWatchedPathInfo.type) {
case BASE:
// Add the output dir to the watch set, scan it, and cancel current watch.
if (event.context().getFileName().toString().equals(OUTPUT)) {
resolvedPath = watchedPath.resolve(event.context());
watchedPathInfo = new WatchedPathInfo(parentWatchedPathInfo, Type.OUTPUT, null);
registerDir(resolvedPath, watchedPathInfo);
// Scan the "output" directory for existing files, and add watches
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(resolvedPath)) {
for (Path path : dirStream) {
// This would be an attempt directory. Add a watch, and track it.
if (path.toFile().isDirectory()) {
watchedPathInfo = new WatchedPathInfo(parentWatchedPathInfo, Type.FINAL, path.getFileName().toString());
registerDir(path, watchedPathInfo);
scanForFinalFiles(watchedPathInfo, path);
} else {
LOG.warn("Ignoring unexpected file: " + path);
}
}
} catch (IOException e) {
LOG.warn("Unable to list files under: " + resolvedPath);
}
// Cancel the watchKey since the output dir has been found.
cancelledWatch = true;
watchKey.cancel();
} else {
LOG.warn("DEBUG: Found unexpected directory while looking for OUTPUT: " + event.context() + " under " + watchedPath);
}
break;
case OUTPUT:
// Add the attemptDir to the watch set, scan it and add to the list of found files
resolvedPath = watchedPath.resolve(event.context());
// New attempt path crated. Add a watch on it, and scan it for existing files.
watchedPathInfo = new WatchedPathInfo(parentWatchedPathInfo, Type.FINAL, event.context().getFileName().toString());
registerDir(resolvedPath, watchedPathInfo);
scanForFinalFiles(watchedPathInfo, resolvedPath);
break;
case FINAL:
resolvedPath = watchedPath.resolve(event.context());
if (event.context().getFileName().toString().equals(ShuffleHandler.DATA_FILE_NAME)) {
registerFoundAttempt(parentWatchedPathInfo.pathIdentifier, null, resolvedPath);
} else if (event.context().getFileName().toString().equals(ShuffleHandler.INDEX_FILE_NAME)) {
registerFoundAttempt(parentWatchedPathInfo.pathIdentifier, resolvedPath, null);
} else {
LOG.warn("Ignoring unexpected file: " + watchedPath.resolve(event.context()));
}
break;
}
}
if (!cancelledWatch) {
boolean valid = watchKey.reset();
if (!valid) {
LOG.warn("DEBUG: WatchKey: " + watchKey.watchable() + " no longer valid");
}
}
}
}
use of java.nio.file.WatchEvent in project quasar by puniverse.
the class ActorLoader method monitorFilesystem.
private static void monitorFilesystem(ActorLoader instance, Path moduleDir) {
try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
moduleDir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
LOG.info("Filesystem monitor: Watching module directory " + moduleDir + " for changes.");
for (; ; ) {
final WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
final WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW) {
// An OVERFLOW event can occur regardless of registration if events are lost or discarded.
LOG.warn("Filesystem monitor: filesystem events may have been missed");
continue;
}
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
// The filename is the context of the event.
final Path filename = ev.context();
// Resolve the filename against the directory.
final Path child = moduleDir.resolve(filename);
if (isValidFile(child, kind == ENTRY_DELETE)) {
try {
final URL jarUrl = child.toUri().toURL();
LOG.info("Filesystem monitor: detected module file {} {}", child, kind == ENTRY_CREATE ? "created" : kind == ENTRY_MODIFY ? "modified" : kind == ENTRY_DELETE ? "deleted" : null);
if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY)
instance.reloadModule(jarUrl);
else if (kind == ENTRY_DELETE)
instance.unloadModule(jarUrl);
} catch (Exception e) {
LOG.error("Filesystem monitor: exception while processing " + child, e);
}
} else {
if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY)
LOG.warn("Filesystem monitor: A non-jar item " + child.getFileName() + " has been placed in the modules directory " + moduleDir);
}
}
if (!key.reset())
throw new IOException("Directory " + moduleDir + " is no longer accessible");
}
} catch (Exception e) {
LOG.error("Filesystem monitor thread terminated with an exception", e);
throw Exceptions.rethrow(e);
}
}
use of java.nio.file.WatchEvent in project tomee by apache.
the class FileWatcher method watch.
public Closeable watch(final String folder) {
final File file = new File(folder);
if (!file.isDirectory()) {
throw new IllegalArgumentException(folder + " is not a directory");
}
try {
final AtomicBoolean again = new AtomicBoolean(true);
final Path path = file.getAbsoluteFile().toPath();
final WatchService watchService = path.getFileSystem().newWatchService();
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
final Thread watcherThread = new Thread(new Runnable() {
@Override
public void run() {
while (again.get()) {
try {
// don't use take to not block forever
final WatchKey key = watchService.poll(1, TimeUnit.SECONDS);
if (key == null) {
continue;
}
for (final WatchEvent<?> event : key.pollEvents()) {
final WatchEvent.Kind<?> kind = event.kind();
if (kind != StandardWatchEventKinds.ENTRY_CREATE && kind != StandardWatchEventKinds.ENTRY_DELETE && kind != StandardWatchEventKinds.ENTRY_MODIFY) {
continue;
}
final Path updatedPath = Path.class.cast(event.context());
if (kind == StandardWatchEventKinds.ENTRY_DELETE || updatedPath.toFile().isFile()) {
final String path = updatedPath.toString();
if (path.endsWith("___jb_tmp___") || path.endsWith("___jb_old___")) {
continue;
} else if (path.endsWith("~")) {
onChange(path.replace(File.pathSeparatorChar, '/').substring(0, path.length() - 1));
} else {
onChange(path.replace(File.pathSeparatorChar, '/'));
}
}
}
key.reset();
} catch (final InterruptedException e) {
Thread.interrupted();
again.set(false);
} catch (final ClosedWatchServiceException cwse) {
// ok, we finished there
}
}
}
});
watcherThread.setName("livereload-tomee-watcher(" + folder + ")");
watcherThread.start();
return new Closeable() {
@Override
public void close() throws IOException {
synchronized (this) {
for (final Session s : sessions) {
removeSession(s);
try {
s.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "container shutdowned"));
} catch (final Exception e) {
// ok: not important there
}
}
}
again.compareAndSet(true, false);
try {
watchService.close();
} catch (final IOException ioe) {
logger.warning("Error closing the watch service for " + folder + "(" + ioe.getMessage() + ")");
}
try {
watcherThread.join(TimeUnit.MINUTES.toMillis(1));
} catch (final InterruptedException e) {
Thread.interrupted();
}
}
};
} catch (final IOException e) {
throw new IllegalArgumentException(e);
}
}
use of java.nio.file.WatchEvent in project tomee by apache.
the class ArchivingTest method logAndRotateAndPurge.
@SuppressWarnings("unchecked")
@Test
public void logAndRotateAndPurge() throws Exception {
clean("target/ArchivingTestPurge-" + format + "/logs");
final AtomicReference<String> today = new AtomicReference<>();
final Map<String, String> config = new HashMap<>();
// initial config
today.set("2015-09-01");
config.put("filenamePattern", "target/ArchivingTestPurge-" + format + "/logs/test.%s.%d.log");
config.put("archiveDirectory", "target/ArchivingTestPurge-" + format + "/logs/archives");
config.put("archiveFormat", format);
config.put("archiveOlderThan", "1 s");
config.put("purgeOlderThan", "2 s");
config.put("limit", "10 kilobytes");
config.put("level", "INFO");
config.put("dateCheckInterval", "1 second");
final LocalFileHandler handler = new LocalFileHandler() {
@Override
protected String currentDate() {
return today.get();
}
@Override
protected String getProperty(final String name, final String defaultValue) {
final String s = config.get(name.substring(name.lastIndexOf('.') + 1));
return s != null ? s : defaultValue;
}
};
final String string10chars = "abcdefghij";
final int iterations = 950;
for (int i = 0; i < iterations; i++) {
handler.publish(new LogRecord(Level.INFO, string10chars));
}
today.set("2015-09-02");
try {
Thread.sleep(2000);
} catch (final InterruptedException e) {
Thread.interrupted();
}
final File logArchive = new File("target/ArchivingTestPurge-" + format + "/logs/archives/test.2015-09-01.0.log." + format);
final File parentFile = logArchive.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
Assert.fail("Unable to create: " + parentFile);
}
final Path dir = parentFile.toPath();
final WatchService watcher = FileSystems.getDefault().newWatchService();
final WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
latch.set(new CountDownLatch(1));
watch(key);
// will trigger the archiving
handler.publish(new LogRecord(Level.INFO, string10chars));
assertTrue("Failed to get archived log", latch.get().await(20, TimeUnit.SECONDS));
final WatchEvent<?> watchEvent = lastEvent.get();
assertTrue(StandardWatchEventKinds.ENTRY_CREATE.equals(watchEvent.kind()) || StandardWatchEventKinds.ENTRY_MODIFY.equals(watchEvent.kind()));
final WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent;
final String io = ev.context().toString();
assertTrue(io.startsWith("test.2015-09-01.") && io.endsWith(format));
today.set("2015-09-03");
try {
Thread.sleep(2500);
} catch (final InterruptedException e) {
Thread.interrupted();
}
// will trigger the purging
handler.publish(new LogRecord(Level.INFO, string10chars));
handler.close();
withRetry(10, 2, new Runnable() {
@Override
public void run() {
assertFalse(logArchive.getAbsolutePath() + " was purged", logArchive.exists());
}
});
}
use of java.nio.file.WatchEvent in project Orchid by JavaEden.
the class FileWatcher method processEvents.
private void processEvents() {
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
Path dir = keys.get(key);
if (dir == null) {
Clog.e("WatchKey not recognized!!");
continue;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = dir.resolve(name);
context.broadcast(Orchid.Lifecycle.FilesChanged.fire(this));
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
try {
if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
registerAll(child);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
boolean valid = key.reset();
if (!valid) {
keys.remove(key);
if (keys.isEmpty()) {
break;
}
}
}
}
Aggregations