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


Java WatchKey.reset方法代码示例

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


在下文中一共展示了WatchKey.reset方法的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: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@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;
        }
    }
}
 
开发者ID:DevopsJK,项目名称:SuitAgent,代码行数:25,代码来源:PluginPropertiesWatcher.java

示例3: 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

示例4: 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

示例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包/类
@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

示例7: 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

示例8: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
public void run() {
    if (running.compareAndSet(false, true)) {
        while (running.get()) {
            // wait for key to be signaled
            WatchKey key = null;
            try {
                key = watcher.take();
                handleEvent(key);
            } catch (final InterruptedException e) {
                return;
            } finally {
                /*
                    Reset the key -- this step is critical to receive
                    further watch events. If the key is no longer valid, the directory
                    is inaccessible so exit the loop.
                 */
                final boolean valid = (key != null && key.reset());
                if (!valid) {
                    LOGGER.warn("Directory key is no longer valid. Quitting watcher service");
                    break;
                }
            }
        }
    }

}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:28,代码来源:JsonServiceRegistryConfigWatcher.java

示例9: run

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
public void run() {
    if (this.running.compareAndSet(false, true)) {
        while (this.running.get()) {
            // wait for key to be signaled
            WatchKey key = null;
            try {
                key = this.watcher.take();
                handleEvent(key);
            } catch (final InterruptedException e) {
                return;
            } finally {
                /*
                    Reset the key -- this step is critical to receive
                    further watch events. If the key is no longer valid, the directory
                    is inaccessible so exit the loop.
                 */
                final boolean valid = key != null && key.reset();
                if (!valid) {
                    LOGGER.warn("Directory key is no longer valid. Quitting watcher service");
                }
            }
        }
    }

}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:27,代码来源:ServiceRegistryConfigWatcher.java

示例10: 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

示例11: call

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
public Void call() throws IOException, InterruptedException {
    this.logger.info(String.format("Start watching %s", this.source));

    final WatchService watchService = this.source.getFileSystem().newWatchService();
    final WatchKey expected = this.source.getParent().register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

    for (; ; ) {
        WatchKey actual = watchService.take();

        if (!actual.equals(expected)) {
            this.logger.warning(String.format("Unknown watch key: %s", actual));
            continue;
        }

        for (WatchEvent<?> watchEvent : actual.pollEvents()) {
            Path changed = (Path) watchEvent.context();

            if (!this.source.getFileName().equals(changed)) {
                this.logger.fine(String.format("Discarding unimportant file change: %s", changed));
                continue;
            }

            this.callback.run();
        }

        if (!actual.reset()) {
            this.logger.warning(String.format("Watch key is no longer valid: %s", actual));
            break;
        }
    }

    this.logger.info(String.format("Stop watching %s", this.source));
    return null;
}
 
开发者ID:cloudfoundry,项目名称:java-buildpack-security-provider,代码行数:36,代码来源:FileWatcher.java

示例12: 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

示例13: pollConfigFileForChanges

import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
 * Polls the configuration file for changes.
 */
private void pollConfigFileForChanges() {

	WatchKey key = watcher.poll();
	try {
		if (key == null) {
			return;
		}

		for (WatchEvent<?> event : key.pollEvents()) {
			Kind<?> kind = event.kind();
			if (StandardWatchEventKinds.OVERFLOW == kind) {
				continue;
			}

			// The filename is the context of the event.
			Path filename = (Path) event.context();

			if (configFile.endsWith(filename.toString())) {
				LOG.info("CORS Filter: Detected change in " + configFile + " , configuration reload required");
				reloadRequired = true;
			}
		}
	} finally {
		if (key != null) {
			key.reset();
		}
	}
}
 
开发者ID:sdcuike,项目名称:cors-filter,代码行数:32,代码来源:CORSConfigurationFileWatcher.java

示例14: doInBackground

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@Override
protected Void doInBackground() throws Exception {

	System.out.println("Watcher starting");

	while (!(new File("/tmp/osvdatasimulator")).isDirectory()) {
		Thread.sleep(1000);
	}

	System.out.println("Watcher ready");

	WatchService watchService = FileSystems.getDefault().newWatchService();
	Path directory = Paths.get("/tmp/osvdatasimulator");
	directory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
	WatchKey key = null;

	while (true) {
		key = watchService.take();
		if (key != null) {
			List<WatchEvent<?>> eventList = key.pollEvents();

			for (int i = 0; i < eventList.size(); i++) {
				publish(eventList.get(i).context().toString());
			}

			key.reset();
		}
	}
}
 
开发者ID:telecombretagne,项目名称:osv,代码行数:30,代码来源:OSVDataWatcher.java

示例15: watchTest

import java.nio.file.WatchKey; //导入方法依赖的package包/类
@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 ");
}
 
开发者ID:callakrsos,项目名称:Gargoyle,代码行数:38,代码来源:FileUtilTest.java


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