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


Java WatchService.take方法代码示例

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


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

示例1: watch

import java.nio.file.WatchService; //导入方法依赖的package包/类
private void watch() {
    try {
        WatchService watchService = directoryPath.getFileSystem().newWatchService();
        directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
        while (true) {
            WatchKey watchKey = watchService.take();
            for (final WatchEvent<?> event : watchKey.pollEvents()) {
                takeActionOnChangeEvent(event);
            }
        }

    } catch (InterruptedException interruptedException) {
        System.out.println("Thread got interrupted:" + interruptedException);
    } catch (Exception exception) {
        exception.printStackTrace();
    }

}
 
开发者ID:patexoid,项目名称:ZombieLib2,代码行数:20,代码来源:DirWatcherService.java

示例2: run

import java.nio.file.WatchService; //导入方法依赖的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.WatchService; //导入方法依赖的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: simpleTest

import java.nio.file.WatchService; //导入方法依赖的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

示例5: poll

import java.nio.file.WatchService; //导入方法依赖的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

示例6: poll

import java.nio.file.WatchService; //导入方法依赖的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

示例7: main

import java.nio.file.WatchService; //导入方法依赖的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

示例8: main

import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
	Path tmpDir = Paths.get("tmp");
	WatchService watchService = FileSystems.getDefault().newWatchService();
	Path monitoredFolder = tmpDir;
	monitoredFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
	
	tmpDir.toFile().mkdirs();
	new FileChanger(tmpDir).start();
	while (true) {
		System.out.println("Waiting for event");
		WatchKey watchKey = watchService.take();
		for (WatchEvent<?> event : watchKey.pollEvents()) {
			System.out.println("Detected event " + event.kind().name() + " on file " + event.context().toString());
		}
		watchKey.reset();
	}
}
 
开发者ID:victorrentea,项目名称:training,代码行数:18,代码来源:FileWatchService.java

示例9: main

import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception  
{  
      
    WatchService watchService=FileSystems.getDefault().newWatchService();  
    Paths.get("C:/").register(watchService,   
            StandardWatchEventKinds.ENTRY_CREATE,  
            StandardWatchEventKinds.ENTRY_DELETE,  
            StandardWatchEventKinds.ENTRY_MODIFY);  
    while(true)  
    {  
        WatchKey key=watchService.take();  
        for(WatchEvent<?> event:key.pollEvents())  
        {  
            System.out.println(event.context()+"发生了"+event.kind()+"事件");  
        }  
        if(!key.reset())  
        {  
            break;  
        }  
    }
}
 
开发者ID:East196,项目名称:maker,代码行数:22,代码来源:WatchServiceTest.java

示例10: watch

import java.nio.file.WatchService; //导入方法依赖的package包/类
private static void watch() throws Exception
{
	WatchService watchService=FileSystems.getDefault().newWatchService();
	Paths.get("C:/").register(watchService, 
			StandardWatchEventKinds.ENTRY_CREATE,
			StandardWatchEventKinds.ENTRY_DELETE,
			StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);
	
	while(true)
	{
		WatchKey key=watchService.take();
		//watchService.poll(10000, TimeUnit.valueOf("2014-8-26"));
		for(WatchEvent<?> event:key.pollEvents())
		{
			System.out.println(event.context()+"发生了"+event.kind()+"事件"+event.count());
		}
		if(!key.reset())
		{
			break;
		}
	}
}
 
开发者ID:yunshouhu,项目名称:LogAnalyzer,代码行数:23,代码来源:Test.java

示例11: run

import java.nio.file.WatchService; //导入方法依赖的package包/类
@Override
public void run() {
    try {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        propsFileFolder.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            WatchKey wk = watchService.take();
            for (WatchEvent<?> event : wk.pollEvents()) {
                Path changed = (Path) event.context();
                Path changedFile = propsFileFolder.resolve(changed.toString());
                if (changed.getFileName().toString().endsWith(fileName) && Files.exists(changedFile)) {
                    log.info("File '{}' changed. Updating values.", changedFile);
                    limits.tokenBody = FileLoaderUtil.readFileAsString(fileName);
                }
            }
            // reset the key
            boolean valid = wk.reset();
            if (!valid) {
                log.info("Key has been not unregistered.");
            }
        }
    } catch (IOException | InterruptedException e) {
        log.warn("Error monitoring '{}' file. Reloadable properties are not enabled.",
                SERVER_PROPERTIES_FILENAME, e);
    }
}
 
开发者ID:blynkkk,项目名称:blynk-server,代码行数:27,代码来源:FileChangeWatcherWorker.java

示例12: main

import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args)throws Exception {  
     
    //获取当前文件系统的WatchService监控对象  
    WatchService watchService=FileSystems.getDefault().newWatchService();  
    //监听的事件类型,有创建,删除,以及修改  
    //Path file = FileSystems.getDefault().getPath(pathname);
    Paths.get("E:\\女神密电").register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);  
  
  while(true){  
      //获取下一个文件变化事件  
      WatchKey key=watchService.take();  
      for(WatchEvent<?> event:key.pollEvents()){  
            
          System.out.println(event.kind().name()+"文件发生了"+event.context().toString()+"事件"+"此事件发生的次数: "+event.count());  
      }  
      //重设WatchKey  
      boolean valid=key.reset();  
      //监听失败,退出监听  
      if(!valid){  
          break;  
      }  
  }  
}
 
开发者ID:desperado1992,项目名称:distributeTemplate,代码行数:24,代码来源:FileMonitorTest.java

示例13: main

import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
	Path path = Paths.get("/home/proberts/Desktop");
	WatchService watchService = path.getFileSystem().newWatchService();
	
	path.register(watchService,
			StandardWatchEventKinds.ENTRY_CREATE,
               StandardWatchEventKinds.ENTRY_MODIFY,
               StandardWatchEventKinds.ENTRY_DELETE);
	
	while(true) {
		WatchKey watchKey = watchService.take();
		
		for (WatchEvent<?> event : watchKey.pollEvents()) {
               printEvent(event);
           }
		
		if(!watchKey.reset()) {
			watchKey.cancel();
			watchService.close();
		}
	}
}
 
开发者ID:psxpaul,项目名称:EclipseJava7Refresher,代码行数:23,代码来源:Java7RefreshProviderPlugin.java

示例14: initWatcher

import java.nio.file.WatchService; //导入方法依赖的package包/类
private void initWatcher() throws IOException, InterruptedException {
	WatchService watcher = path.getFileSystem().newWatchService();
	path.register(watcher,
			StandardWatchEventKinds.ENTRY_CREATE,
			StandardWatchEventKinds.ENTRY_MODIFY,
			StandardWatchEventKinds.ENTRY_DELETE);

	LOG.info("Now watching template folder: " + path.toFile().getAbsolutePath());

	while (true) {
		WatchKey key = watcher.take();
		List<WatchEvent<?>> events = key.pollEvents();
		if (!events.isEmpty()) {
			updateTemplates();
		}
		key.reset();
	}
}
 
开发者ID:devhub-tud,项目名称:devhub-prototype,代码行数:19,代码来源:TemplateEngine.java

示例15: main

import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
	File file = new File(".");

	Path path = file.toPath();
	final WatchService watcher;
	try {
		watcher = FileSystems.getDefault().newWatchService();
		path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}

	WatchKey watchKey = watcher.take();
	while(true) {
		for (WatchEvent<?> event : watchKey.pollEvents()) {
			System.out.println(event + ", context:" + event.context() + ", kind:" + event.kind() + ", count:" + event.count());
		}
		watchKey.reset();
	}
}
 
开发者ID:sitoolkit,项目名称:sit-ad,代码行数:21,代码来源:WatchServiceTrial.java


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