本文整理汇总了Java中java.nio.file.WatchEvent类的典型用法代码示例。如果您正苦于以下问题:Java WatchEvent类的具体用法?Java WatchEvent怎么用?Java WatchEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WatchEvent类属于java.nio.file包,在下文中一共展示了WatchEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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();
}
}
示例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;
}
示例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;
}
}
}
示例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
);
}
}
}
}
示例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*/ }
}
示例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) {}
}
}
示例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
}
}
}
示例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();
}
}
示例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;
}
示例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
}
}
示例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);
}
}
}
}
示例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;
}
示例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();
}
示例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();
}
}