本文整理汇总了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;
}
示例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;
}
}
}
示例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*/ }
}
示例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;
}
}
示例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);
}
示例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
}
}
}
示例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();
}
示例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");
}
}
}
}
}
示例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();
}
}
}
示例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;
}
示例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 ();
}
}
}
}
示例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();
}
}
}
示例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();
}
}
}
示例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 ");
}