本文整理汇总了Java中java.nio.file.WatchKey.watchable方法的典型用法代码示例。如果您正苦于以下问题:Java WatchKey.watchable方法的具体用法?Java WatchKey.watchable怎么用?Java WatchKey.watchable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.WatchKey
的用法示例。
在下文中一共展示了WatchKey.watchable方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: handleEvent
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
* Handle event.
*
* @param key the key
*/
private void handleEvent(final WatchKey key) {
this.readLock.lock();
try {
for (final WatchEvent<?> event : key.pollEvents()) {
if (event.count() <= 1) {
final WatchEvent.Kind kind = event.kind();
//The filename is the context of the event.
final WatchEvent<Path> ev = (WatchEvent<Path>) event;
final Path filename = ev.context();
final Path parent = (Path) key.watchable();
final Path fullPath = parent.resolve(filename);
final File file = fullPath.toFile();
LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
handleCreateEvent(file);
} else if (kind.name().equals(ENTRY_DELETE.name())) {
handleDeleteEvent();
} else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
handleModifyEvent(file);
}
}
}
} finally {
this.readLock.unlock();
}
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:36,代码来源:JsonServiceRegistryConfigWatcher.java
示例3: awaitChanges
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/** Returns set of files that have been modified. */
private static Set<Path> awaitChanges(WatchService watcher) throws InterruptedException {
Set<Path> paths = new HashSet<>();
WatchKey key = watcher.take();
do {
Path directory = (Path) key.watchable();
for (WatchEvent<?> event : key.pollEvents()) {
paths.add(directory.resolve((Path) event.context()));
}
key.reset();
} while ((key = watcher.poll()) != null);
return paths;
}
示例4: checkKey
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void checkKey ( final WatchKey key ) throws IOException
{
if ( this.baseKey == key )
{
checkBaseEvents ( key, key.pollEvents () );
if ( !this.baseKey.reset () )
{
logger.warn ( "Base key got invalidated" );
this.watcher.close ();
}
}
else
{
final Path base = (Path)key.watchable ();
if ( ! ( key.watchable () instanceof Path ) )
{
// don't reset
return;
}
final StorageWatcher w = this.watcherMap.get ( base );
if ( w == null )
{
logger.info ( "Event for unknown target: {}", base );
// don't reset
return;
}
try
{
for ( final WatchEvent<?> event : key.pollEvents () )
{
if ( ! ( event.context () instanceof Path ) )
{
continue;
}
w.handleEvent ( (Path)key.watchable (), event );
}
}
finally
{
if ( !key.reset () )
{
w.dispose ();
}
}
}
}
示例5: autoDeepScan
import java.nio.file.WatchKey; //导入方法依赖的package包/类
public static void autoDeepScan(int index) throws IOException, InterruptedException {
// ��ȡ�ļ�ϵͳ��WatchService����
final WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get(CentralControl.monitorPath).register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
final File file = new File(CentralControl.monitorPath);
final LinkedList<File> fList = new LinkedList<File>();
fList.addLast(file);
while (fList.size() > 0) {
final File f = fList.removeFirst();
if (f.listFiles() == null)
continue;
for (final File file2 : f.listFiles()) {
if (file2.isDirectory()) {// ��һ��Ŀ¼
fList.addLast(file2);
// ����ע����Ŀ¼
Paths.get(file2.getAbsolutePath()).register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
}
}
}
while (true) {
if (CentralControl.ifStop) {
watchService.poll();
break;
}
// ��ȡ��һ���ļ��Ķ��¼�
final WatchKey key = watchService.take();
for (final WatchEvent<?> event : key.pollEvents()) {
// �ļ�����/ɾ��/��ʱ֪ͨ
// System.out.println(event.context() + " comes to " + event.kind());
final Object eventcontext = event.context();
fileName = Obj2String.o2s(eventcontext);
final Object eventkind = event.kind();
final String eventkindstr = Obj2String.o2s(eventkind);
if (GetPostfix.fromFilename(fileName).equals("jpg")) {
if (eventkindstr.equals("ENTRY_CREATE")) {
System.out.println(fileName + " Created");
System.out.println(key.watchable() + " Modified");
final Object path = key.watchable();
filePath = Obj2String.o2s(path);
System.out.println("Ready 2 be analysed");
switch (index) {
case 1:
AlgoList.godzilla();
break;
case 2:
AlgoList.pythagoras_G();
break;
default:
AlgoList.pythagoras_G();
break;
}
}
}
}
// ����WatchKey
final boolean valid = key.reset();
// �������ʧ�ܣ��˳�����
if (!valid) {
break;
}
}
}
示例6: watch
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void watch() {
try (FileSystem fs = FileSystems.getDefault()) {
try (WatchService ws = fs.newWatchService()) {
Path securityDirectory = securityScanner.getSecurityDirectory();
securityDirectory.register(ws,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
PathMatcher jarPathMatcher = fs.getPathMatcher("glob:*.jar");
LOG.info("Watching security directory for changes - {}", securityDirectory);
while (true) {
WatchKey key = ws.take();
for (WatchEvent<?> we : key.pollEvents()) {
WatchEvent.Kind<?> wek = we.kind();
Path parent = (Path) key.watchable();
Path filename = (Path) we.context();
// ignore event.
if (StandardWatchEventKinds.OVERFLOW.equals(wek)) {
continue;
}
final Path file = parent.resolve(filename);
final boolean isJarFile = jarPathMatcher.matches(filename);
if (isJarFile) {
if (StandardWatchEventKinds.ENTRY_CREATE.equals(wek)
|| StandardWatchEventKinds.ENTRY_MODIFY.equals(wek)) {
securityScanner.scan(file);
} else if (StandardWatchEventKinds.ENTRY_DELETE.equals(wek)) {
securityScanner.remove(file);
}
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
} catch (IOException | InterruptedException e) {
LOG.error("Could not watch security directory for changes", e);
}
}
示例7: watch
import java.nio.file.WatchKey; //导入方法依赖的package包/类
private void watch() {
try (FileSystem fs = FileSystems.getDefault()) {
try (WatchService ws = fs.newWatchService()) {
Path routeDirectory = routeScanner.getRouteDirectory();
routeDirectory.register(ws,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
PathMatcher jarPathMatcher = fs.getPathMatcher("glob:*.jar");
LOG.info("Watching route directory for changes - {}", routeDirectory);
while (true) {
WatchKey key = ws.take();
for (WatchEvent<?> we : key.pollEvents()) {
WatchEvent.Kind<?> wek = we.kind();
Path parent = (Path) key.watchable();
Path filename = (Path) we.context();
// ignore event.
if (StandardWatchEventKinds.OVERFLOW.equals(wek)) {
continue;
}
final Path file = parent.resolve(filename);
final boolean isJarFile = jarPathMatcher.matches(filename);
if (isJarFile) {
if (StandardWatchEventKinds.ENTRY_CREATE.equals(wek)
|| StandardWatchEventKinds.ENTRY_MODIFY.equals(wek)) {
routeScanner.scan(file);
} else if (StandardWatchEventKinds.ENTRY_DELETE.equals(wek)) {
routeScanner.remove(file);
}
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
}
} catch (IOException | InterruptedException e) {
LOG.error("Could not watch route directory for changes", e);
}
}
示例8: internal_watchDirectoryPath
import java.nio.file.WatchKey; //导入方法依赖的package包/类
/**
* Internal watch directory path method.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
private void internal_watchDirectoryPath() throws IOException {
WatchKey key = null;
boolean stopPolling = false;
// Poll for events in an infinite loop
for (;;) {
try {
// The take method waits till watch service receives a
// notification
key = watchService.take();
} catch (InterruptedException e) {
// Ignore
}
// once a key is obtained, we poll for events on that key
List<WatchEvent<?>> keys = key.pollEvents();
for (WatchEvent<?> watchEvent : keys) {
Kind<?> watchEventKind = watchEvent.kind();
// Sometimes events are created faster than they are registered
// or the implementation may specify a maximum number of events
// and further events are discarded. In these cases an event of
// kind overflow is returned. We ignore this case for now
if (watchEventKind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
Path dir = (Path) key.watchable();
Path fullPath = dir.resolve((Path) watchEvent.context());
FileWatcherUpdateInterface parentObj = watcherMap.get(key);
if (watchEventKind == StandardWatchEventKinds.ENTRY_CREATE) {
// A new file has been created
if (parentObj != null) {
parentObj.fileAdded(fullPath);
}
} else if (watchEventKind == StandardWatchEventKinds.ENTRY_MODIFY) {
ReloadManager.getInstance().fileModified(fullPath);
// The file has been modified.
if (parentObj != null) {
parentObj.fileModified(fullPath);
}
} else if (watchEventKind == StandardWatchEventKinds.ENTRY_DELETE) {
if (parentObj != null) {
parentObj.fileDeleted(fullPath);
}
}
// Reset the key so the further key events may be polled
key.reset();
}
if (stopPolling) {
break;
}
}
// Close the watcher service
watchService.close();
}