java.nio.file.WatchEvent Java Examples

The following examples show how to use java.nio.file.WatchEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileObservable.java    From rxjava-file with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if and only if the path corresponding to a WatchEvent
 * represents the given file. This will be the case for Create, Modify,
 * Delete events.
 * 
 * @param file
 *            the file to restrict events to
 * @return predicate
 */
private final static Func1<WatchEvent<?>, Boolean> onlyRelatedTo(final File file) {
    return new Func1<WatchEvent<?>, Boolean>() {

        @Override
        public Boolean call(WatchEvent<?> event) {

            final boolean ok;
            if (file.isDirectory())
                ok = true;
            else if (StandardWatchEventKinds.OVERFLOW.equals(event.kind()))
                ok = true;
            else {
                Object context = event.context();
                if (context != null && context instanceof Path) {
                    Path p = (Path) context;
                    Path basePath = getBasePath(file);
                    File pFile = new File(basePath.toFile(), p.toString());
                    ok = pFile.getAbsolutePath().equals(file.getAbsolutePath());
                } else
                    ok = false;
            }
            return ok;
        }
    };
}
 
Example #2
Source File: DefaultCertificateValidator.java    From opc-ua-stack with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            WatchKey key = watchService.take();

            if (key == trustedKey) {
                for (WatchEvent<?> watchEvent : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = watchEvent.kind();

                    if (kind != StandardWatchEventKinds.OVERFLOW) {
                        synchronizeTrustedCertificates();
                    }
                }
            }

            if (!key.reset()) {
                break;
            }
        } catch (InterruptedException e) {
            logger.error("Watcher interrupted.", e);
        }
    }
}
 
Example #3
Source File: RecursiveFSEventProcessor.java    From singer with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the file system event is a create event
 */
public void processFileSystemEvent(FileSystemEvent event) throws Exception {
  Path absolutePath = event.logDir();
  String podUid = extractPodUidFromPath(absolutePath,
      SingerSettings.getSingerConfig().getKubeConfig().getPodLogDirectory());

  WatchEvent.Kind<?> kind = event.event().kind();
  Path path = (Path) event.event().context();

  // resolve path because WatchService returns event Paths that are relative
  path = event.logDir().resolve(path);

  boolean isCreate = kind.equals(StandardWatchEventKinds.ENTRY_CREATE);
  if (isCreate) {
    evaluateAndRegisterLogStreamOrWatcher(path, podUid);
  }
}
 
Example #4
Source File: LiveDirs.java    From LiveDirsFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void processEvent(Path dir, WatchEvent<Path> event) {
    // Context for directory entry event is the file name of entry
    Path relChild = event.context();
    Path child = dir.resolve(relChild);

    Kind<Path> kind = event.kind();

    if(kind == ENTRY_MODIFY) {
        handleModification(child, externalInitiator);
    } else if(kind == ENTRY_CREATE) {
        handleCreation(child, externalInitiator);
    } else if(kind == ENTRY_DELETE) {
        model.delete(child, externalInitiator);
    } else {
        throw new AssertionError("unreachable code");
    }
}
 
Example #5
Source File: WindowsWatchService.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
Example #6
Source File: WatchQueueReader.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
private Path resolvePath(WatchKey key, WatchEvent<?> event) {
    WatchEvent<Path> ev = cast(event);
    // Context for directory entry event is the file name of entry.
    Path contextPath = ev.context();
    Path baseWatchedDir = null;
    Path registeredPath = null;
    synchronized (this) {
        baseWatchedDir = keyToService.get(key).getSourcePath();
        registeredPath = registeredKeys.get(key);
    }
    if (registeredPath != null) {
        // If the path has been registered in the watch service it relative path can be resolved
        // The context path is resolved by its already registered parent path
        return registeredPath.resolve(contextPath);
    }

    logger.warn(
            "Detected invalid WatchEvent '{}' and key '{}' for entry '{}' in not registered file or directory of '{}'",
            event, key, contextPath, baseWatchedDir);
    return null;
}
 
Example #7
Source File: FileWatcher.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
private void callback(final Local folder, final WatchEvent<?> event, final FileWatcherListener l) {
    final WatchEvent.Kind<?> kind = event.kind();
    if(log.isInfoEnabled()) {
        log.info(String.format("Process file system event %s for %s", kind.name(), event.context()));
    }
    if(ENTRY_MODIFY == kind) {
        l.fileWritten(this.normalize(folder, event.context().toString()));
    }
    else if(ENTRY_DELETE == kind) {
        l.fileDeleted(this.normalize(folder, event.context().toString()));
    }
    else if(ENTRY_CREATE == kind) {
        l.fileCreated(this.normalize(folder, event.context().toString()));
    }
    else {
        log.debug(String.format("Ignored file system event %s for %s", kind.name(), event.context()));
    }
}
 
Example #8
Source File: PluginPropertiesWatcher.java    From OpenFalcon-SuitAgent with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    WatchService watchService = WatchServiceUtil.watchModify(pluginDir);
    WatchKey key;
    while (watchService != null){
        try {
            key = watchService.take();
            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                if(watchEvent.kind() == ENTRY_MODIFY){
                    String fileName = watchEvent.context() == null ? "" : watchEvent.context().toString();
                    Plugin plugin = PluginLibraryHelper.getPluginByConfigFileName(fileName);
                    if(plugin != null){
                        plugin.init(PluginLibraryHelper.getPluginConfig(plugin));
                        log.info("已完成插件{}的配置重新加载",plugin.pluginName());
                    }
                }
            }
            key.reset();
        } catch (Exception e) {
            log.error("插件配置文件监听异常",e);
            break;
        }
    }
}
 
Example #9
Source File: AbstractWatchServiceTest.java    From jimfs with Apache License 2.0 6 votes vote down vote up
@Test
public void testOverflow() throws IOException {
  AbstractWatchService.Key key =
      watcher.register(new StubWatchable(), ImmutableSet.of(ENTRY_CREATE));
  for (int i = 0; i < AbstractWatchService.Key.MAX_QUEUE_SIZE + 10; i++) {
    key.post(new AbstractWatchService.Event<>(ENTRY_CREATE, 1, null));
  }
  key.signal();

  List<WatchEvent<?>> events = key.pollEvents();

  assertThat(events).hasSize(AbstractWatchService.Key.MAX_QUEUE_SIZE + 1);
  for (int i = 0; i < AbstractWatchService.Key.MAX_QUEUE_SIZE; i++) {
    assertThat(events.get(i).kind()).isEqualTo(ENTRY_CREATE);
  }

  WatchEvent<?> lastEvent = events.get(AbstractWatchService.Key.MAX_QUEUE_SIZE);
  assertThat(lastEvent.kind()).isEqualTo(OVERFLOW);
  assertThat(lastEvent.count()).isEqualTo(10);
}
 
Example #10
Source File: FileWatcher.java    From jfilter with Apache License 2.0 6 votes vote down vote up
/**
 * Process all modify events
 *
 * @throws InterruptedException if interrupted while waiting
 */
@SuppressWarnings("unchecked")
private void processModifiedFiles() throws InterruptedException {
    WatchKey key = watcher.take();
    for (WatchEvent<?> event : key.pollEvents()) {
        WatchEvent.Kind<?> kind = event.kind();


        if (kind == StandardWatchEventKinds.OVERFLOW) {
            overflowed = true;
            continue;
        }

        if (watchKeys.containsKey(key)) {
            WatchEvent<Path> ev = (WatchEvent<Path>) event;

            String filename = String.format("%s%s%s", watchKeys.get(key).toString(),
                    File.separator, ev.context().toString());
            File file = new File(filename);

            if (fileIsModified(file))
                fileRecords.get(file).onEvent();
        }
    }
    key.reset();
}
 
Example #11
Source File: FileModificationEventWatcher.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param fileToWatch path of the file to watch
 * @param callback          the callback to invoke when a modification to the {@code fileToWatch} is detected
 * @param loopContinuously  whether to keep continue to look for file modification after one iteration. This option
 *                          is useful for testing only.
 * @throws InvalidPathException if {@code fileToWatch} is invalid
 * @throws FileNotFoundException when a file at specified path {@code fileToWatch} does not exist
 * @throws NullPointerException if either {@code fileToWatch} or {@code callback} is null
 */
@VisibleForTesting
FileModificationEventWatcher(@NonNull Path fileToWatch, @NonNull Consumer<WatchEvent<?>> callback,
                             boolean loopContinuously, boolean checkForFileExistence)
        throws FileNotFoundException {

    // Set the name for this object/thread for identification purposes.
    super("pravega-file-watcher-" + THREAD_NUM.incrementAndGet());
    Exceptions.checkNotNullOrEmpty(fileToWatch.toString(), "fileToWatch");
    if (checkForFileExistence && !fileToWatch.toFile().exists()) {
        throw new FileNotFoundException(String.format("File [%s] does not exist.", fileToWatch));
    }

    this.watchedFilePath = fileToWatch;
    this.callback = callback;
    this.loopContinuously = loopContinuously;
    setUncaughtExceptionHandler(uncaughtExceptionalHandler);
}
 
Example #12
Source File: WindowsWatchService.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
WindowsWatchKey init(long handle,
                     Set<? extends WatchEvent.Kind<?>> events,
                     boolean watchSubtree,
                     NativeBuffer buffer,
                     long countAddress,
                     long overlappedAddress,
                     int completionKey)
{
    this.handle = handle;
    this.events = events;
    this.watchSubtree = watchSubtree;
    this.buffer = buffer;
    this.countAddress = countAddress;
    this.overlappedAddress = overlappedAddress;
    this.completionKey = completionKey;
    return this;
}
 
Example #13
Source File: Main.java    From java-cheat with GNU General Public License v3.0 6 votes vote down vote up
public static void main(final String[] args) throws InterruptedException, IOException {
    final WatchService watchService = FileSystems.getDefault().newWatchService();
    Paths.get(WATCH_DIR).register(
            watchService,
            StandardWatchEventKinds.ENTRY_CREATE,
            StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY);
    for (;;) {
        final WatchKey key = watchService.take();
        for (final WatchEvent<?> event : key.pollEvents()) {
            final WatchEvent.Kind kind = event.kind();
            // TODO
            if (kind == StandardWatchEventKinds.OVERFLOW) continue;
            System.out.format("%s: %s\n", kind.name(), cast(event).context());
        }
        key.reset();
    }
}
 
Example #14
Source File: WindowsWatchService.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
WindowsWatchKey init(long handle,
                     Set<? extends WatchEvent.Kind<?>> events,
                     boolean watchSubtree,
                     NativeBuffer buffer,
                     long countAddress,
                     long overlappedAddress,
                     int completionKey)
{
    this.handle = handle;
    this.events = events;
    this.watchSubtree = watchSubtree;
    this.buffer = buffer;
    this.countAddress = countAddress;
    this.overlappedAddress = overlappedAddress;
    this.completionKey = completionKey;
    return this;
}
 
Example #15
Source File: WatchQueueReader.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private Path resolvePath(WatchKey key, WatchEvent<?> event) {
    WatchEvent<Path> ev = cast(event);
    // Context for directory entry event is the file name of entry.
    Path contextPath = ev.context();
    Path baseWatchedDir = null;
    Path registeredPath = null;
    synchronized (this) {
        baseWatchedDir = keyToService.get(key).getSourcePath();
        registeredPath = registeredKeys.get(key);
    }
    if (registeredPath != null) {
        // If the path has been registered in the watch service it relative path can be resolved
        // The context path is resolved by its already registered parent path
        return registeredPath.resolve(contextPath);
    }

    logger.warn(
            "Detected invalid WatchEvent '{}' and key '{}' for entry '{}' in not registered file or directory of '{}'",
            event, key, contextPath, baseWatchedDir);
    return null;
}
 
Example #16
Source File: AbstractConfigService.java    From java-trader with Apache License 2.0 5 votes vote down vote up
private void watchThreadFunc() {
    logger.info("Config watch thread is started");
    while(state!=ServiceState.Stopped) {
        WatchKey watchKey = null;
        try{
            watchKey = watcher.poll(100, TimeUnit.MILLISECONDS);
        }catch(Throwable t) {}
        if ( watchKey==null ) {
            Thread.yield();
            continue;
        }
        ConfigProviderEntry providerEntry = getEntryByFile(watchKey);
        if ( providerEntry!=null ) {
            for(WatchEvent<?> event:watchKey.pollEvents()) {
                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                WatchEvent.Kind<?> kind = event.kind();
                Path filename = ev.context();
                if (kind == java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY
                && filename.toString().equals(providerEntry.file.getName())) {
                    doReload(providerEntry);
                }
            }
        }
        watchKey.reset();
    }
    logger.info("Config watch thread is stopped");
}
 
Example #17
Source File: WindowsWatchService.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
WatchKey register(Path path,
                  WatchEvent.Kind<?>[] events,
                  WatchEvent.Modifier... modifiers)
     throws IOException
{
    // delegate to poller
    return poller.register(path, events, modifiers);
}
 
Example #18
Source File: BaseWatcher.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public void handleEvent ( final Path watchable, final WatchEvent<?> event ) throws IOException
{
    final Path path = (Path)event.context ();
    logger.debug ( "Change {} for base: {} on {}", new Object[] { event.kind (), watchable, path } );

    if ( watchable.endsWith ( "native" ) && path.toString ().equals ( "settings.xml" ) )
    {
        if ( event.kind () != StandardWatchEventKinds.ENTRY_DELETE )
        {
            check ();
        }
        else
        {
            storageRemoved ();
        }
    }
    else
    {
        if ( path.toString ().equals ( "settings.xml" ) )
        {
            if ( event.kind () == StandardWatchEventKinds.ENTRY_CREATE )
            {
                this.nativeKey = new File ( watchable.toFile (), "native" ).toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE );
            }
        }
        else if ( path.toString ().endsWith ( ".hds" ) )
        {
            if ( this.id != null )
            {
                this.storageManager.fileChanged ( this.path.toFile (), this.id, path.toFile () );
            }
        }
    }
}
 
Example #19
Source File: FolderReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void addFileToQueue(WatchKey key, WatchEvent<?> event, WatchEvent<Path> pathEvent) {
  getMonitor()
      .debug(
          event.kind().name() + " event received - file '{}' will be added to the queue",
          pathEvent.context());
  try {
    Path dir = watchKeys.get(key);
    if (dir != null) {
      Path resolved = dir.resolve(pathEvent.context());
      if (resolved.toFile().isDirectory()) {
        if (recursive) {
          addFilesFromDir(resolved.toFile());
          registerDirectory(resolved);
        }
      } else {
        addFile(resolved);
      }
    } else {
      getMonitor()
          .warn(
              "WatchKey not found - file '{}' will not be added to the queue",
              pathEvent.context());
    }
  } catch (Exception ioe) {
    getMonitor()
        .warn(
            "An error occurred - file '{}' will not be added to the queue",
            pathEvent.context(),
            ioe);
  }
}
 
Example #20
Source File: AbstractWatchKey.java    From directory-watcher with Apache License 2.0 5 votes vote down vote up
@Override
public List<WatchEvent<?>> pollEvents() {
  // note: it's correct to be able to retrieve more events from a key without calling reset()
  // reset() is ONLY for "returning" the key to the watch service to potentially be retrieved by
  // another thread when you're finished with it
  List<WatchEvent<?>> result = new ArrayList<>(events.size());
  events.drainTo(result);
  int overflowCount = overflow.getAndSet(0);
  if (overflowCount != 0) {
    result.add(overflowEvent(overflowCount));
  }
  return Collections.unmodifiableList(result);
}
 
Example #21
Source File: AbstractWatchKey.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds the event to this key and signals it.
 *
 * @param kind    event kind
 * @param context context
 */
@SuppressWarnings("unchecked")
final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
    synchronized(this) {
        int size = events.size();
        if(size > 1) {
            // don't let list get too big
            if(size >= MAX_EVENT_LIST_SIZE) {
                kind = StandardWatchEventKinds.OVERFLOW;
                context = null;
            }

            // repeated event
            WatchEvent<?> prev = events.get(size - 1);
            if(kind == prev.kind()) {
                boolean isRepeat;
                if(context == null) {
                    isRepeat = (prev.context() == null);
                }
                else {
                    isRepeat = context.equals(prev.context());
                }
                if(isRepeat) {
                    ((Event<?>) prev).increment();
                    return;
                }
            }
        }

        // non-repeated event
        events.add(new Event<Object>((WatchEvent.Kind<Object>) kind, context));
        signal();
    }
}
 
Example #22
Source File: TestWatchService.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test(expected=ClosedWatchServiceException.class)
@Ignore
public void testSimpleEx() throws IOException {
  Path rootPath = Paths.get(clusterUri);
  
  WatchService watcher = rootPath.getFileSystem().newWatchService();
  rootPath.register(watcher, 
        new WatchEvent.Kind<?>[] { ENTRY_MODIFY });
  watcher.close();
  // Should throw ClosedWatchServiceException
  watcher.poll();
}
 
Example #23
Source File: Watcher.java    From mangooio with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("all")
private void handleEvents(WatchKey watchKey, Path path) {
    for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
        WatchEvent.Kind<?> watchEventKind = watchEvent.kind();
        if (OVERFLOW.equals(watchEventKind)) {
            continue;
        }

        WatchEvent<Path> ev = (WatchEvent<Path>) watchEvent;
        Path name = ev.context();
        Path child = path.resolve(name);

        if (ENTRY_MODIFY.equals(watchEventKind) && !child.toFile().isDirectory()) {
            handleNewOrModifiedFile(child);
        }

        if (ENTRY_CREATE.equals(watchEventKind)) {
            if (!child.toFile().isDirectory()) {
                handleNewOrModifiedFile(child);
            }
            try {
                if (Files.isDirectory(child, NOFOLLOW_LINKS)) {
                    registerAll(child);
                }
            } catch (IOException e) {
                LOG.error("Something fishy happened. Unable to register new dir for watching", e);
            }
        }
    }
}
 
Example #24
Source File: MacOSXListeningWatchService.java    From RxJavaFileUtils with Apache License 2.0 5 votes vote down vote up
@Override
WatchKey register(WatchableFile watchableFile, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifers) throws IOException {
    final File file = watchableFile.toFile();
    final Map<File, Long> lastModifiedMap = createLastModifiedMap(file);
    final String s = file.getAbsolutePath();
    final Pointer[] values = {CFStringRef.toCFString(s).getPointer()};
    final CFArrayRef pathsToWatch = CarbonAPI.INSTANCE.CFArrayCreate(null, values, CFIndex.valueOf(1), null);
    final MacOSXWatchKey watchKey = new MacOSXWatchKey(watchableFile, this, events);

    final double latency = 1.0; /* Latency in seconds */

    final long kFSEventStreamEventIdSinceNow = -1; //  this is 0xFFFFFFFFFFFFFFFF
    final int kFSEventStreamCreateFlagNoDefer = 0x00000002;
    final CarbonAPI.FSEventStreamCallback callback = new MacOSXListeningCallback(watchKey, lastModifiedMap);
    callbackList.add(callback);
    final FSEventStreamRef stream = CarbonAPI.INSTANCE.FSEventStreamCreate(
            Pointer.NULL,
            callback,
            Pointer.NULL,
            pathsToWatch,
            kFSEventStreamEventIdSinceNow,
            latency,
            kFSEventStreamCreateFlagNoDefer);

    final CFRunLoopThread thread = new CFRunLoopThread(stream, file);
    thread.setDaemon(true);
    thread.start();
    threadList.add(thread);
    return watchKey;
}
 
Example #25
Source File: AbstractWatchService.java    From jimfs with Apache License 2.0 5 votes vote down vote up
@Override
public List<WatchEvent<?>> pollEvents() {
  // note: it's correct to be able to retrieve more events from a key without calling reset()
  // reset() is ONLY for "returning" the key to the watch service to potentially be retrieved by
  // another thread when you're finished with it
  List<WatchEvent<?>> result = new ArrayList<>(events.size());
  events.drainTo(result);
  int overflowCount = overflow.getAndSet(0);
  if (overflowCount != 0) {
    result.add(overflowEvent(overflowCount));
  }
  return Collections.unmodifiableList(result);
}
 
Example #26
Source File: WindowsWatchService.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void processEvents(WindowsWatchKey key, int size) {
    long address = key.buffer().address();

    int nextOffset;
    do {
        int action = UNSAFE.getInt(address + OFFSETOF_ACTION);

        // map action to event
        WatchEvent.Kind<?> kind = translateActionToEvent(action);
        if (key.events().contains(kind)) {
            // copy the name
            int nameLengthInBytes = UNSAFE.getInt(address + OFFSETOF_FILENAMELENGTH);
            if ((nameLengthInBytes % 2) != 0) {
                throw new AssertionError("FileNameLength is not a multiple of 2");
            }
            char[] nameAsArray = new char[nameLengthInBytes/2];
            UNSAFE.copyMemory(null, address + OFFSETOF_FILENAME, nameAsArray,
                Unsafe.ARRAY_CHAR_BASE_OFFSET, nameLengthInBytes);

            // create FileName and queue event
            WindowsPath name = WindowsPath
                .createFromNormalizedPath(fs, new String(nameAsArray));
            key.signalEvent(kind, name);
        }

        // next event
        nextOffset = UNSAFE.getInt(address + OFFSETOF_NEXTENTRYOFFSET);
        address += (long)nextOffset;
    } while (nextOffset != 0);
}
 
Example #27
Source File: CsvFolderReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Every time doHasNext() is called, check the WatchService for new events and add all new events
 * to the queue. Then return true if there are files on the queue, or false otherwise.
 *
 * <p>If the event indicates that a file has been deleted, ensure it is removed from the queue.
 */
@Override
public boolean doHasNext() throws IOException, CollectionException {
  WatchKey key;
  while ((key = watcher.poll()) != null) {
    for (WatchEvent<?> event : key.pollEvents()) {
      processEvent(key, event);
      getMonitor().meter("events").mark();
    }

    key.reset();
  }

  return !currLines.isEmpty() || !queue.isEmpty();
}
 
Example #28
Source File: Main.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static void processEvents(File watchedDir, Path file) {
    for (;;) {

        WatchKey key;
        try {
            key = watcher.take();
        } catch (InterruptedException x) {
            return;
        }

        for (WatchEvent<?> event: key.pollEvents()) {
            Kind<?> kind = event.kind();

            WatchEvent<Path> ev = (WatchEvent<Path>)event;
            Path name = ev.context();
            Path child = watchedDir.toPath().resolve(name);

            if (kind == ENTRY_DELETE && child.equals(file)) {
                return;
            }
        }

        boolean valid = key.reset();
        if (!valid) {
           break;
        }
    }
}
 
Example #29
Source File: DirectoryChangeProcessor.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Constructor for DirectoryChangeProcessor
 * @param datastreamTask The datastream task this processor is responsible for
 * @param producer The event producer this connector uses to send change events
 *                 to the underlying {@link com.linkedin.datastream.server.api.transport.TransportProvider}.
 * @throws IOException if an I/O error occurs
 */
public DirectoryChangeProcessor(DatastreamTask datastreamTask, DatastreamEventProducer producer) throws IOException {
  Validate.notNull(datastreamTask);
  Validate.notNull(producer);

  final String path = datastreamTask.getDatastreamSource().getConnectionString();
  Validate.isTrue(isDirectory(path), "path does not refer to a valid directory");

  _task = datastreamTask;
  _producer = producer;
  _dirPath = Paths.get(path);
  _watchService = FileSystems.getDefault().newWatchService();
  _watchKey = _dirPath.register(_watchService, new WatchEvent.Kind<?>[] {ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE},
      SensitivityWatchEventModifier.HIGH);
}
 
Example #30
Source File: ErrorWatcher.java    From weMessage with GNU Affero General Public License v3.0 5 votes vote down vote up
public void run(){
    isRunning.set(true);

    clearErroredFiles();

    try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
        final WatchKey watchKey = FileSystems.getDefault().getPath(serverConfiguration.getParentDirectoryPath()).register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_CREATE}, SensitivityWatchEventModifier.HIGH);

        while (isRunning.get()) {
            final WatchKey wk = watchService.take();

            for (WatchEvent<?> event : wk.pollEvents()) {
                final Path changed = (Path) event.context();

                if (changed.toFile().getName().startsWith(SCRIPT_ERROR_FILE_PREFIX)) {
                    processError(ErrorFileType.SCRIPT, changed.toFile());
                }
            }
            boolean valid = wk.reset();
            if (!valid) {
                ServerLogger.log(ServerLogger.Level.INFO, TAG, "The watcher key has been unregistered");
            }
        }
    }catch(Exception ex){
        if (isRunning.get()) {
            ServerLogger.error(TAG, "An error occurred while watching for errors. Shutting down!", ex);
            messageServer.shutdown(-1, false);
        }
    }
}