use of java.nio.file.WatchService in project camel by apache.
the class FileWatcherReloadStrategy method doStart.
@Override
protected void doStart() throws Exception {
super.doStart();
if (folder == null) {
// no folder configured
return;
}
File dir = new File(folder);
if (dir.exists() && dir.isDirectory()) {
log.info("Starting ReloadStrategy to watch directory: {}", dir);
WatchEvent.Modifier modifier = null;
// if its mac OSX then attempt to apply workaround or warn its slower
String os = ObjectHelper.getSystemProperty("os.name", "");
if (os.toLowerCase(Locale.US).startsWith("mac")) {
// this modifier can speedup the scanner on mac osx (as java on mac has no native file notification integration)
Class<WatchEvent.Modifier> clazz = getCamelContext().getClassResolver().resolveClass("com.sun.nio.file.SensitivityWatchEventModifier", WatchEvent.Modifier.class);
if (clazz != null) {
WatchEvent.Modifier[] modifiers = clazz.getEnumConstants();
for (WatchEvent.Modifier mod : modifiers) {
if ("HIGH".equals(mod.name())) {
modifier = mod;
break;
}
}
}
if (modifier != null) {
log.info("On Mac OS X the JDK WatchService is slow by default so enabling SensitivityWatchEventModifier.HIGH as workaround");
} else {
log.warn("On Mac OS X the JDK WatchService is slow and it may take up till 10 seconds to notice file changes");
}
}
try {
Path path = dir.toPath();
WatchService watcher = path.getFileSystem().newWatchService();
// we cannot support deleting files as we don't know which routes that would be
if (modifier != null) {
path.register(watcher, new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_MODIFY }, modifier);
} else {
path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
}
task = new WatchFileChangesTask(watcher, path);
executorService = getCamelContext().getExecutorServiceManager().newSingleThreadExecutor(this, "FileWatcherReloadStrategy");
executorService.submit(task);
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
}
use of java.nio.file.WatchService in project Gargoyle by callakrsos.
the class FileUtilTest method watchTest.
@Test
public void watchTest() throws IOException {
File file = new File("c:\\someDir");
file.mkdirs();
WatchService newWatchService = FileSystems.getDefault().newWatchService();
WatchKey register = file.toPath().register(newWatchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watch Service Registered ..");
while (true) {
try {
System.out.println("start");
WatchKey key = newWatchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (key == ENTRY_MODIFY && fileName.toString().equals("DirectoryWatchDemo.java")) {
System.out.println("My source file has changed!!!");
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
} catch (InterruptedException e) {
break;
}
}
System.out.println("end ");
}
use of java.nio.file.WatchService in project opennms by OpenNMS.
the class DirectoryWatcher method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
@SuppressWarnings("unchecked")
public void run() {
LOG.info("starting run loop");
try (WatchService watcher = m_path.getFileSystem().newWatchService()) {
LOG.debug("registering create watcher on {}", m_path.toAbsolutePath().toString());
m_path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
LOG.debug("watcher registration complete for {}", m_path.toAbsolutePath().toString());
synchronized (this) {
this.notifyAll();
}
while (true) {
if (m_thread.isInterrupted()) {
break;
}
WatchKey key = null;
try {
LOG.debug("waiting for create event");
key = watcher.take();
LOG.debug("got an event, process it");
} catch (InterruptedException ie) {
LOG.info("interruped, must be time to shut down...");
break;
}
for (WatchEvent<?> watchEvent : key.pollEvents()) {
WatchEvent.Kind<?> kind = watchEvent.kind();
Path pathChanged = ((WatchEvent<Path>) watchEvent).context();
final String fileName = pathChanged.toString();
final File file = new File(m_directory, fileName);
if (file.isDirectory()) {
// Ignoring changes on directories.
LOG.debug("{} is a directory, ignoring.", file);
continue;
}
if (kind == StandardWatchEventKinds.OVERFLOW) {
LOG.debug("overflow receiving, ignoring changes.");
continue;
} else if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
LOG.info("file '{}' created. Ignoring...", fileName);
} else if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
LOG.info("file '{}' modified. Removing entry from cache.", fileName);
m_contents.remove(fileName);
} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
LOG.info("file '{}' deleted. Removing entry from cache.", fileName);
m_contents.remove(fileName);
}
// IMPORTANT: The key must be reset after processed
if (!key.reset()) {
break;
}
}
}
} catch (IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
LOG.info("existing run loop");
}
use of java.nio.file.WatchService in project spf4j by zolyfarkas.
the class TSDBReader method watch.
// CHECKSTYLE:OFF
@SuppressFBWarnings("NOS_NON_OWNED_SYNCHRONIZATION")
public <E extends Exception> void watch(final Handler<Either<TableDef, DataBlock>, E> handler, final EventSensitivity es) throws IOException, InterruptedException, E {
// CHECKSTYLE:ON
synchronized (this) {
if (watch) {
throw new IllegalStateException("File is already watched " + file);
}
watch = true;
}
SensitivityWatchEventModifier sensitivity;
switch(es) {
case LOW:
sensitivity = SensitivityWatchEventModifier.LOW;
break;
case MEDIUM:
sensitivity = SensitivityWatchEventModifier.MEDIUM;
break;
case HIGH:
sensitivity = SensitivityWatchEventModifier.HIGH;
break;
default:
throw new UnsupportedOperationException("Unsupported sensitivity " + es);
}
final Path path = file.getParentFile().toPath();
try (WatchService watchService = path.getFileSystem().newWatchService()) {
path.register(watchService, new WatchEvent.Kind[] { StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW }, sensitivity);
readAll(handler);
do {
WatchKey key = watchService.poll(1000, TimeUnit.MILLISECONDS);
if (key == null) {
if (reReadSize()) {
readAll(handler);
}
continue;
}
if (!key.isValid()) {
key.cancel();
break;
}
if (!key.pollEvents().isEmpty()) {
if (reReadSize()) {
readAll(handler);
}
}
if (!key.reset()) {
key.cancel();
break;
}
} while (watch);
} finally {
watch = false;
}
}
use of java.nio.file.WatchService in project cryptofs by cryptomator.
the class CryptoPathTest method testRegisterWithThreeParamsThrowsUnsupportedOperationException.
@Test
public void testRegisterWithThreeParamsThrowsUnsupportedOperationException() throws IOException {
WatchService irrelevantWatcher = null;
WatchEvent.Kind<?>[] irrelevantWatchEvents = null;
WatchEvent.Modifier[] irrelevantModifiers = null;
thrown.expect(UnsupportedOperationException.class);
rootPath.register(irrelevantWatcher, irrelevantWatchEvents, irrelevantModifiers);
}
Aggregations