• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java WatchEvent类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



WatchEvent类属于java.nio.file包,在下文中一共展示了WatchEvent类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: init

import java.nio.file.WatchEvent; //导入依赖的package包/类
WindowsWatchKey init(long handle,
                     Set<? extends WatchEvent.Kind<?>> events,
                     boolean watchSubtree,
                     NativeBuffer buffer,
                     long countAddress,
                     long overlappedAddress,
                     int completionKey)
{
    this.handle = handle;
    this.events = events;
    this.watchSubtree = watchSubtree;
    this.buffer = buffer;
    this.countAddress = countAddress;
    this.overlappedAddress = overlappedAddress;
    this.completionKey = completionKey;
    return this;
}
 
开发者ID:ojdkbuild,项目名称:lookaside_java-1.8.0-openjdk,代码行数:18,代码来源:WindowsWatchService.java


示例2: watch

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


示例3: nextEvent

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


示例4: run

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


示例5: processSubevents

import java.nio.file.WatchEvent; //导入依赖的package包/类
/**
 * Processes subevents of the key.
 * @param key That has new events.
 * @param dir For the key.
 * @throws IOException If a subdirectory cannot be registered.
 */
private void processSubevents(final WatchKey key, final Path dir)
    throws IOException {
    for (final WatchEvent event : key.pollEvents()) {
        final WatchEvent.Kind kind = event.kind();
        final Path name = (Path) event.context();
        final Path child = dir.resolve(name);
        Logger.debug(
            this,
            "%s: %s%n", event.kind().name(), child
        );
        if (kind == ENTRY_CREATE) {
            try {
                if (Files.isDirectory(child)) {
                    this.processSubevents(child);
                }
            } catch (final IOException ex) {
                throw new IOException(
                    "Failed to register subdirectories.",
                    ex
                );
            }
        }
    }
}
 
开发者ID:driver733,项目名称:VKMusicUploader,代码行数:31,代码来源:WatchDirs.java


示例6: setWatcherOnThemeFile

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


示例7: run

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


示例8: run

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


示例9: simpleTest

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


示例10: MacOSXWatchKey

import java.nio.file.WatchEvent; //导入依赖的package包/类
public MacOSXWatchKey(final Watchable file, final FSEventWatchService service, final WatchEvent.Kind<?>[] events) {
    super(service);
    this.file = file;

    boolean reportCreateEvents = false;
    boolean reportModifyEvents = false;
    boolean reportDeleteEvents = false;

    for(WatchEvent.Kind<?> event : events) {
        if(event == StandardWatchEventKinds.ENTRY_CREATE) {
            reportCreateEvents = true;
        }
        else if(event == StandardWatchEventKinds.ENTRY_MODIFY) {
            reportModifyEvents = true;
        }
        else if(event == StandardWatchEventKinds.ENTRY_DELETE) {
            reportDeleteEvents = true;
        }
    }
    this.reportCreateEvents = reportCreateEvents;
    this.reportDeleteEvents = reportDeleteEvents;
    this.reportModifyEvents = reportModifyEvents;
}
 
开发者ID:iterate-ch,项目名称:cyberduck,代码行数:24,代码来源:FSEventWatchService.java


示例11: translateActionToEvent

import java.nio.file.WatchEvent; //导入依赖的package包/类
private WatchEvent.Kind<?> translateActionToEvent(int action) {
    switch (action) {
        case FILE_ACTION_MODIFIED :
            return StandardWatchEventKinds.ENTRY_MODIFY;

        case FILE_ACTION_ADDED :
        case FILE_ACTION_RENAMED_NEW_NAME :
            return StandardWatchEventKinds.ENTRY_CREATE;

        case FILE_ACTION_REMOVED :
        case FILE_ACTION_RENAMED_OLD_NAME :
            return StandardWatchEventKinds.ENTRY_DELETE;

        default :
            return null;  // action not recognized
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:WindowsWatchService.java


示例12: processWatchEvent

import java.nio.file.WatchEvent; //导入依赖的package包/类
/**
 * Handles a watchevent, it currently only handles the DELETE and MODIFY - create isn't handled
 * as when you create a new file you get two events - one of the create and one for the modify,
 * therefore it can be safely handled in the modify only.
 *
 * If the delete corresponds to a adapter loaded - it deregisterWithAdapterManager it with the
 * adatperManager If the modify is new / modified it passes it to the load method that handles
 * re-registering existing adapters
 *
 * @param event
 */
public void processWatchEvent(final WatchEvent<?> event) {
    synchronized (propertiesToAdapter) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Logging watch event:" + event.kind() + ": " + event.context());
        }
        // check it is ended with .properties
        if (isPropertyFile(event)) {
            String path = ((Path) event.context()).toString();
            Adapter adapter = propertiesToAdapter.get(path);
            // if we have already seen this then deregister it
            if (adapter != null) {
                removeAdapter(path, adapter);
            }

            if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE
                    || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                File file = new File(getPathToWatch().toString(), ((Path) event.context()).toString());
                load(file);
            }
        }
    }
}
 
开发者ID:HewlettPackard,项目名称:loom,代码行数:34,代码来源:AdapterLoader.java


示例13: createMockWatchKeyForPath

import java.nio.file.WatchEvent; //导入依赖的package包/类
private WatchKey createMockWatchKeyForPath(String configFilePath) {
    final WatchKey mockWatchKey = Mockito.mock(WatchKey.class);
    final List<WatchEvent<?>> mockWatchEvents = (List<WatchEvent<?>>) Mockito.mock(List.class);
    when(mockWatchKey.pollEvents()).thenReturn(mockWatchEvents);
    when(mockWatchKey.reset()).thenReturn(true);

    final Iterator mockIterator = Mockito.mock(Iterator.class);
    when(mockWatchEvents.iterator()).thenReturn(mockIterator);

    final WatchEvent mockWatchEvent = Mockito.mock(WatchEvent.class);
    when(mockIterator.hasNext()).thenReturn(true, false);
    when(mockIterator.next()).thenReturn(mockWatchEvent);

    // In this case, we receive a trigger event for the directory monitored, and it was the file monitored
    when(mockWatchEvent.context()).thenReturn(Paths.get(configFilePath));
    when(mockWatchEvent.kind()).thenReturn(ENTRY_MODIFY);

    return mockWatchKey;
}
 
开发者ID:apache,项目名称:nifi-minifi,代码行数:20,代码来源:FileChangeIngestorTest.java


示例14: listenEvent

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


示例15: processEvent

import java.nio.file.WatchEvent; //导入依赖的package包/类
/** Notify file system event. */
void processEvent() {
	while(true) {
		WatchKey signal;
		
		try {
			signal = watcher.take();
		} catch (InterruptedException e) {
			return;
		}
		
		for(WatchEvent<?> event : signal.pollEvents()) {
			Kind<?> kind = event.kind();
			
			if(kind == StandardWatchEventKinds.OVERFLOW) {
				continue;
			}
			
			Path name = (Path)event.context();
			notify(name.toAbsolutePath().toString(), kind);
		}
		
		key.reset();
	}
}
 
开发者ID:yoking-zhang,项目名称:demo,代码行数:26,代码来源:DirectoryWatcher.java


示例16: watch

import java.nio.file.WatchEvent; //导入依赖的package包/类
public void watch() throws IOException, InterruptedException {
  LOG.info("Watching directory {} for event(s) {}", watchDirectory, watchEvents);

  WatchKey watchKey = watchDirectory.register(watchService, watchEvents.toArray(new WatchEvent.Kind[watchEvents.size()]));

  while (!stopped) {
    if (watchKey != null) {

      processWatchKey(watchKey);

      if (!watchKey.reset()) {
        LOG.warn("WatchKey for {} no longer valid", watchDirectory);
        break;
      }
    }

    watchKey = watchService.poll(pollWaitCheckShutdownMillis, TimeUnit.MILLISECONDS);
  }
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:20,代码来源:WatchServiceHelper.java


示例17: processWatchKey

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


示例18: main

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


示例19: handleEvent

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


示例20: run

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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java JMXConnectorFactory类代码示例发布时间:2022-05-20
下一篇:
Java RenderHelper类代码示例发布时间:2022-05-20
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap