当前位置: 首页>>代码示例>>Java>>正文


Java WatchKey.pollEvents方法代码示例

本文整理汇总了Java中java.nio.file.WatchKey.pollEvents方法的典型用法代码示例。如果您正苦于以下问题:Java WatchKey.pollEvents方法的具体用法?Java WatchKey.pollEvents怎么用?Java WatchKey.pollEvents使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.file.WatchKey的用法示例。


在下文中一共展示了WatchKey.pollEvents方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: nextEvent

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
protected String nextEvent() throws IOException, InterruptedException {
    WatchKey key;
    try {
        key = watcher.take();
    } catch (ClosedWatchServiceException cwse) { // #238261
        @SuppressWarnings({"ThrowableInstanceNotThrown"})
        InterruptedException ie = new InterruptedException();
        throw (InterruptedException) ie.initCause(cwse);
    }
    Path dir = (Path)key.watchable();
           
    String res = dir.toAbsolutePath().toString();
    for (WatchEvent<?> event: key.pollEvents()) {
        if (event.kind() == OVERFLOW) {
            // full rescan
            res = null;
        }
    }
    key.reset();
    return res;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:23,代码来源:NioNotifier.java

示例2: simpleTest

import java.nio.file.WatchKey; //导入方法依赖的package包/类
public void simpleTest(Path path) throws Exception
{
	WatchService watchService=FileSystems.getDefault().newWatchService();  
	path.register(watchService,   
            StandardWatchEventKinds.ENTRY_CREATE,  
            StandardWatchEventKinds.ENTRY_DELETE,  
            StandardWatchEventKinds.ENTRY_MODIFY);  
    while(true)  
    {  
        WatchKey watchKey=watchService.take();  
           List<WatchEvent<?>> watchEvents = watchKey.pollEvents();  
           for(WatchEvent<?> event : watchEvents){  
               //TODO 根据事件类型采取不同的操作。。。。。。。  
               System.out.println("["+event.context()+"]文件发生了["+event.kind()+"]事件");    
           }  
           watchKey.reset(); 
    } 
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:19,代码来源:FileMonitorJdkImpl.java

示例3: poll

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(WatchService watcher) {
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:22,代码来源:LotsOfCancels.java

示例4: setWatcherOnThemeFile

import java.nio.file.WatchKey; //导入方法依赖的package包/类
private static void setWatcherOnThemeFile() {
    try {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            final WatchKey wk = watchService.take();
            for (WatchEvent<?> event : wk.pollEvents()) {
                //we only register "ENTRY_MODIFY" so the context is always a Path.
                final Path changed = (Path) event.context();
                System.out.println(changed);
                if (changed.endsWith("Theme.css")) {
                    System.out.println("Theme.css has changed...reloading stylesheet.");
                    scene.getStylesheets().clear();
                    scene.getStylesheets().add("resources/Theme.css");
                }
            }
            boolean valid = wk.reset();
            if (!valid)
                System.out.println("Watch Key has been reset...");
        }
    } catch (Exception e) { /*Thrown to void*/ }
}
 
开发者ID:maximstewart,项目名称:UDE,代码行数:23,代码来源:UFM.java

示例5: poll

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(int id, WatchService watcher) {
    System.out.printf("begin poll %d%n", id);
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do but print
        System.out.printf("poll %d expected exception %s%n", id, expected);
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
    System.out.printf("end poll %d%n", id);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:LotsOfCancels.java

示例6: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void run() {
	while (running) {
		try {
			final WatchKey watchKey = watcher.take();
			for (final WatchEvent<?> event : watchKey.pollEvents()) {
				Path changed = (Path) event.context();
				if (changed == null || event.kind() == StandardWatchEventKinds.OVERFLOW) {
					System.out.println("bad file watch event: " + event);
					continue;
				}
				changed = watchedDirectory.resolve(changed);
				for (final ListenerAndPath x : listeners) {
					if (Thread.interrupted() && !running)
						return;
					if (changed.startsWith(x.startPath)) {
						x.listener.fileChanged(changed, (Kind<Path>) event.kind());
					}
				}
			}
			watchKey.reset();
		} catch (final InterruptedException e) {}
	}
}
 
开发者ID:Njol,项目名称:Motunautr,代码行数:26,代码来源:FileWatcher.java

示例7: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
protected void run() {
    while(isRunning()) {
        try {
            final WatchKey key = watchService.take();
            final WatchedDirectory watchedDirectory = dirsByKey.get(key);
            if(watchedDirectory == null) {
                logger.warning("Cancelling unknown key " + key);
                key.cancel();
            } else {
                for(WatchEvent<?> event : key.pollEvents()) {
                    watchedDirectory.dispatch((WatchEvent<Path>) event);
                }
                key.reset();
            }
        } catch(InterruptedException e) {
            // ignore, just check for termination
        }
    }
}
 
开发者ID:OvercastNetwork,项目名称:ProjectAres,代码行数:21,代码来源:PathWatcherServiceImpl.java

示例8: processWatchKey

import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void processWatchKey(WatchKey watchKey) throws IOException {
  final long start = System.currentTimeMillis();
  final List<WatchEvent<?>> events = watchKey.pollEvents();

  int processed = 0;

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

    if (!watchEvents.contains(kind)) {
      LOG.trace("Ignoring an {} event to {}", event.context());
      continue;
    }

    WatchEvent<Path> ev = cast(event);
    Path filename = ev.context();

    if (processEvent(kind, filename)) {
      processed++;
    }
  }

  LOG.debug("Handled {} out of {} event(s) for {} in {}", processed, events.size(), watchDirectory, JavaUtils.duration(start));
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:25,代码来源:WatchServiceHelper.java

示例9: handleEvent

import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void handleEvent(final WatchKeyHolder watchKeys, final WatchKey key) throws IOException {
  for (final WatchEvent<?> event : key.pollEvents()) {
    if (event.kind() == StandardWatchEventKinds.OVERFLOW) {
      continue;
    }

    final WatchEvent<Path> watchEvent = cast(event);
    Path path = watchKeys.get(key);
    if (path == null) {
      continue;
    }

    path = path.resolve(watchEvent.context());
    if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
      if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
        watchKeys.register(path);
      }
    } else {
      // Dispatch
      FileEvent fe = toEvent(watchEvent, path);
      if (fe != null) {
        this.eventBus.post(fe);
      }
    }
  }
}
 
开发者ID:mopemope,项目名称:meghanada-server,代码行数:27,代码来源:FileSystemWatcher.java

示例10: listenEvent

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * 是否监听事件
 * 
 * @return 是否监听
 */
private boolean listenEvent() {
    WatchKey signal;
    try {
        Thread.sleep(500L);
        signal = watchService.take();
    }
    catch (InterruptedException e) {
        return false;
    }

    for (WatchEvent<?> event : signal.pollEvents()) {
        log.info("event:" + event.kind() + "," + "filename:" + event.context());
        pushEvent(event);
    }

    return signal.reset();
}
 
开发者ID:ErinDavid,项目名称:elastic-config,代码行数:23,代码来源:LocalFileListenerManager.java

示例11: main

import java.nio.file.WatchKey; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    String tempDirPath = "/tmp";
    System.out.println("Starting watcher for " + tempDirPath);
    Path p = Paths.get(tempDirPath);
    WatchService watcher = 
        FileSystems.getDefault().newWatchService();
    Kind<?>[] watchKinds = { ENTRY_CREATE, ENTRY_MODIFY };
    p.register(watcher, watchKinds);
    mainRunner = Thread.currentThread();
    new Thread(new DemoService()).start();
    while (!done) {
        WatchKey key = watcher.take();
        for (WatchEvent<?> e : key.pollEvents()) {
            System.out.println(
                "Saw event " + e.kind() + " on " + 
                e.context());
            if (e.context().toString().equals("MyFileSema.for")) {
                System.out.println("Semaphore found, shutting down watcher");
                done = true;
            }
        }
        if (!key.reset()) {
            System.err.println("Key failed to reset!");
        }
    }
}
 
开发者ID:shashanksingh28,项目名称:code-similarity,代码行数:27,代码来源:FileWatchServiceDemo.java

示例12: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
public void run() {
    Logger.info(TAG, "Modified %s STARTED", pin.getCode());
    while (!closed) {
        WatchKey key = null;
        try {
            key = service.take();
        } catch (InterruptedException e) {
        }
        if (this.key.equals(key)) {
            for (WatchEvent<?> event : key.pollEvents()) {
                WatchEvent.Kind<?> kind = event.kind();
                if (!kind.equals(ENTRY_MODIFY) || !event.context().toString().equals(pin.getPath().getName()))
                    continue;
                onChange(pin);
            }
        }
        boolean valid = key.reset();
        if (!valid) {
            break;
        }
    }
}
 
开发者ID:serayuzgur,项目名称:heartbeat,代码行数:23,代码来源:WatcherPinListener.java

示例13: handleEvent

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * Handle event.
 *
 * @param key the key
 */
private void handleEvent(final WatchKey key) {
    this.readLock.lock();
    try {
        for (final WatchEvent<?> event : key.pollEvents()) {
            if (event.count() <= 1) {
                final WatchEvent.Kind kind = event.kind();

                //The filename is the context of the event.
                final WatchEvent<Path> ev = (WatchEvent<Path>) event;
                final Path filename = ev.context();

                final Path parent = (Path) key.watchable();
                final Path fullPath = parent.resolve(filename);
                final File file = fullPath.toFile();

                LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
                if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
                    handleCreateEvent(file);
                } else if (kind.name().equals(ENTRY_DELETE.name())) {
                    handleDeleteEvent();
                } else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
                    handleModifyEvent(file);
                }
            }

        }
    } finally {
        this.readLock.unlock();
    }
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:36,代码来源:JsonServiceRegistryConfigWatcher.java

示例14: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * A method to run the threads
 */
public void run() {
    
    // thread loop
    while (!Thread.currentThread().isInterrupted()) {
        
        try {
            // get the key from watcher
            WatchKey key = folderWatcher.take();

            WatchEvent<?> tempEvent;
            // get all events
            ArrayList<WatchEvent<?>> allEvents = (ArrayList<WatchEvent<?>>) key.pollEvents();
                            
            // iterate over events
            for (int i = 0; i < allEvents.size(); i++) {
                tempEvent = allEvents.get(i);
                mainFrame.updateProjects();
            }
            
            // reset the key
            key.reset();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
开发者ID:bufferhe4d,项目名称:call-IDE,代码行数:31,代码来源:RealTimeFolderWatcher.java

示例15: scanner

import java.nio.file.WatchKey; //导入方法依赖的package包/类
protected void scanner ()
{
    logger.trace ( "Watching for events" );
    while ( true )
    {
        WatchKey key = null;
        try
        {
            key = this.ws.take ();
            logger.trace ( "Took events: {}", key.watchable () );

            final List<WatchEvent<?>> events = key.pollEvents ();
            for ( final WatchEvent<?> evt : events )
            {
                processEvent ( evt );
            }
        }
        catch ( final InterruptedException | ClosedWatchServiceException e )
        {
            return;
        }
        finally
        {
            if ( key != null )
            {
                key.reset ();
            }
        }
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:31,代码来源:AbstractMergeWatcher.java


注:本文中的java.nio.file.WatchKey.pollEvents方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。