Package java.nio.file

Examples of java.nio.file.WatchKey


      }
    }
  }

  private synchronized void unregisterWatch(Path dir) {
    WatchKey watchKey = watchPathKeyMap.get(dir);

    if (watchKey != null) {
      logger.log(Level.INFO, "- Cancelling " + dir);
     
      watchKey.cancel();
      watchPathKeyMap.remove(dir);
    }
  }
View Full Code Here


    FileSystem fileSystem = directory.getFileSystem();

    WatchService watcher = fileSystem.newWatchService();

    try {
      WatchKey key = directory.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
      if(key.isValid()){
        process(key);
      }

      while(key.reset()){

        try {
          key = watcher.take();
        } catch(InterruptedException ie){
          break;
        }

        if(key.isValid()){
          process(key);
        }
      }
    } finally {
      watcher.close();
View Full Code Here

  /**
   * Register the given directory with the WatchService
   */
  private void registerRoot() throws IOException {
    WatchKey key = this.folder
        .register(watcher, ENTRY_CREATE, ENTRY_DELETE);
    this.keyModuleMap.put(key, "");

  }
View Full Code Here

    this.keyModuleMap.put(key, "");

  }

  private void register(Path dir, String moduleName) throws IOException {
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY,
        ENTRY_DELETE);
    this.keyModuleMap.put(key, moduleName);

    System.out.printf("%s, %s -> %s\n", moduleName, key, dir);
  }
View Full Code Here

  }

  private void watch() {
    boolean exit = false;
    while (!exit) {
      WatchKey key;

      try {
        key = this.watcher.take();
      } catch (InterruptedException e) {
        return;
View Full Code Here

     * @param path
     *            the path
     */
    protected void register(Path path) {
        try {
            WatchKey key = path.register(fsWatchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
            pathByKey.put(key, path);
        } catch (IOException e) {
            throw new StLightException(e);
        }
    }
View Full Code Here

    @Override
    public void run() {
        for (;;) {

            // Wait for the next event
            WatchKey key;
            try {
                key = fsWatchService.take();
            } catch (InterruptedException e) {
                throw new StLightException(e);
            }

            // Go through all the events
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent<Path> pathEvent = (WatchEvent<Path>) event;

                // Check the path
                Path completePath = pathByKey.get(key).resolve(pathEvent.context());
                File completeFile = completePath.toFile();

                // Check the event
                WatchEvent.Kind<Path> kind = pathEvent.kind();
                if (StandardWatchEventKinds.OVERFLOW.equals(kind)) {
                    continue;
                } else if (StandardWatchEventKinds.ENTRY_CREATE.equals(kind)) {
                    // Check if is a directory and we want to register it
                    if (recursive && Files.isDirectory(completePath, LinkOption.NOFOLLOW_LINKS)) {
                        registerRecursively(completePath);
                    }

                    for (FileSystemUpdateHandler handler : fileSystemUpdateHandlers) {
                        handler.created(completeFile);
                    }
                } else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(kind)) {
                    for (FileSystemUpdateHandler handler : fileSystemUpdateHandlers) {
                        handler.modified(completeFile);
                    }
                } else if (StandardWatchEventKinds.ENTRY_DELETE.equals(kind)) {
                    for (FileSystemUpdateHandler handler : fileSystemUpdateHandlers) {
                        handler.deleted(completeFile);
                    }
                }

            }

            // Reset
            if (!key.reset()) {
                pathByKey.remove(key);
            }

        }
    }
View Full Code Here

    @Override
    public void run() {
      try {
        for (;;) {
          WatchKey key = null;
          try {
            key = watchService.take();
          } catch (InterruptedException e) {
            return;
          }

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

            if (kind == OVERFLOW) {
              continue;
            }

            // Context for directory entry event is the file name of
            // entry
            WatchEvent<Path> ev = cast(event);
            Path name = ev.context();

            // print out event
            if (kind == ENTRY_CREATE || kind == ENTRY_MODIFY) {
              try {
                processConfigFile(new File(getServiceConfigFolder() + File.separator + name.toString()));
              } catch (IOException e) {
                logger.warn("Could not process config file '{}': {}", name, e);
              }
            }
          }
          key.reset();
        }
      } catch (ClosedWatchServiceException ecx) {
        logger.debug("Terminated thread {}", Thread.currentThread().getName());
        return;
      }
View Full Code Here

        }
    }
    private static void watch(){
        try {
            while (true) {
                final WatchKey key = watchService.take();
                if(key == null){
                    continue;
                }
                for (WatchEvent<?> watchEvent : key.pollEvents()) {
                    final WatchEvent.Kind<?> kind = watchEvent.kind();
                    //忽略无效事件
                    if (kind == StandardWatchEventKinds.OVERFLOW) {
                        continue;
                    }
                    final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
                    //path是相对路径(相对于监控目录)
                    final Path contextPath = watchEventPath.context();
                    LOG.debug("contextPath:"+contextPath);
                    //获取监控目录
                    final Path directoryPath = directories.get(key);
                    LOG.debug("directoryPath:"+directoryPath);
                    //得到绝对路径
                    final Path absolutePath = directoryPath.resolve(contextPath);
                    LOG.debug("absolutePath:"+absolutePath);
                    LOG.debug("kind:"+kind);
                    //判断事件类别
                    switch (kind.name()) {
                        case "ENTRY_CREATE":
                            if (Files.isDirectory(absolutePath, LinkOption.NOFOLLOW_LINKS)) {
                                LOG.info("新增目录:" + absolutePath);
                                LOG.info("Create directory:" + absolutePath, Locale.ENGLISH);
                                //为新增的目录及其所有子目录注册监控事件
                                registerTree(absolutePath);
                            }else{
                                LOG.info("新增文件:" + absolutePath);
                                LOG.info("Create file:" + absolutePath, Locale.ENGLISH);                               
                            }
                            break;
                        case "ENTRY_DELETE":
                            LOG.info("删除:" + absolutePath);
                            LOG.info("Delete:" + absolutePath, Locale.ENGLISH);
                            break;
                    }
                }
                boolean valid = key.reset();
                if (!valid) {
                    LOG.info("停止监控目录:"+directories.get(key));
                    directories.remove(key);
                    if (directories.isEmpty()) {
                        LOG.error("退出监控");
View Full Code Here

     * @param path
     * @throws IOException
     */
    private static void registerPath(Path path) throws IOException {
        LOG.debug("监控目录:" + path);
        WatchKey key = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_DELETE);
        directories.put(key, path);
    }
View Full Code Here

TOP

Related Classes of java.nio.file.WatchKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.