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


Java WatchService.poll方法代码示例

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


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

示例1: testWatchKeyPollEventsEmptiesQue

import java.nio.file.WatchService; //导入方法依赖的package包/类
@Test
@Category( { SlowTest.class, Watchable.class, Writable.class } )
public void testWatchKeyPollEventsEmptiesQue() throws Exception {
    Path dir = dirTA();
    Path toBeDeleted = dirTAB();
    final WatchService watcher = dir.getFileSystem().newWatchService();
    dir.register( watcher, ENTRY_DELETE );

    Thread.sleep( 1000 );
    Files.delete( toBeDeleted );

    waitForWatchService();

    WatchKey watchKey = watcher.poll();
    watchKey.pollEvents();
    assertThat( watchKey.pollEvents() ).isEmpty();
}
 
开发者ID:openCage,项目名称:niotest,代码行数:18,代码来源:Tests11Watcher.java

示例2: awaitChanges

import java.nio.file.WatchService; //导入方法依赖的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;
}
 
开发者ID:bazelbuild,项目名称:rules_closure,代码行数:14,代码来源:Metadata.java

示例3: watchFileChange

import java.nio.file.WatchService; //导入方法依赖的package包/类
public void watchFileChange() throws IOException, InterruptedException {
	final Path path = Paths.get(directoryPath);
	final WatchService watchService = 
			path.getFileSystem().newWatchService();
	path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
	System.out.println("Report any file changed within next 1 minute ...");
	
	final WatchKey watchKey = watchService.poll(1, TimeUnit.MINUTES);
	if (watchKey != null) {
		watchKey.pollEvents()
		.stream()
		.forEach(event -> System.out.println(event.context()));
	}
}
 
开发者ID:anilpank,项目名称:java8examples,代码行数:15,代码来源:FileExamples.java

示例4: testSimpleEx

import java.nio.file.WatchService; //导入方法依赖的package包/类
@Test(expected=ClosedWatchServiceException.class)
@Ignore
public void testSimpleEx() throws IOException {
  Path rootPath = Paths.get(clusterUri);
  
  WatchService watcher = rootPath.getFileSystem().newWatchService();
  rootPath.register(watcher, 
        new WatchEvent.Kind<?>[] { ENTRY_MODIFY });
  watcher.close();
  // Should throw ClosedWatchServiceException
  watcher.poll();
}
 
开发者ID:damiencarol,项目名称:jsr203-hadoop,代码行数:13,代码来源:TestWatchService.java

示例5: testEntryCreate

import java.nio.file.WatchService; //导入方法依赖的package包/类
public void testEntryCreate() throws IOException, InterruptedException, StfException {
	Path tempDirectory = getTemporaryDirectory();
	WatchService watchService = FileSystems.getDefault().newWatchService();
	try {
		tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
		Path newFilename = Paths.get("ENTRY_CREATE.txt");
		Path newFile = tempDirectory.resolve(newFilename);
		Files.createFile(newFile);
		assertTrue("File was not created", Files.exists(newFile));

		WatchKey key = null;

		// We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got something by
		// then we assume failure
		key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);

		assertNotNull("Polling WatchService object returned null", key);

		boolean eventFound = false;

		// Check for exactly one event
		for (WatchEvent<?> event : key.pollEvents()) {
			if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) {
				// Assert exactly one event
				assertFalse("Duplicate ENTRY_CREATE events delivered for one file creation", eventFound);
				// Assert filename is correct
				assertEquals(newFilename, (Path) event.context());
				eventFound = true;
			} else {
				fail(event.kind() + " event retured, expected ENTRY_CREATE");
			}
		}

		// Reset the key, to allow for future events
		key.reset();

	} finally {
		watchService.close();
	}
	okForCleanup();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:42,代码来源:DirectoryWatcherTest.java

示例6: testMultipleEntryCreate

import java.nio.file.WatchService; //导入方法依赖的package包/类
public void testMultipleEntryCreate() throws IOException, InterruptedException, StfException {
	Path tempDirectory = getTemporaryDirectory();
	WatchService watchService = FileSystems.getDefault().newWatchService();
	try {
		tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
		WatchKey key = null;
		for (int iteration = 0; iteration < 10; iteration++) {
			HangNotifier.ping();
			Path newFilename = Paths.get("ENTRY_CREATE" + iteration + ".txt");
			Path newFile = tempDirectory.resolve(newFilename);
			Files.createFile(newFile);
			assertTrue("File was not created", Files.exists(newFile));

			key = null;

			// We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got something by
			// then we assume failure

			key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);
			assertNotNull("Polling WatchService object returned null", key);
			
			boolean eventFound = false;

			// Check for exactly one event
			for (WatchEvent<?> event : key.pollEvents()) {
				if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) {
					// Assert exactly one event
					assertFalse("Duplicate ENTRY_CREATE events delivered for one file creation", eventFound);
					// Assert filename is correct
					assertEquals(newFilename, (Path) event.context());
					eventFound = true;
				} else {
					fail(event.kind() + " event retured, expected ENTRY_CREATE");
				}
			}

			// Reset the key, to allow for future events
			if (key != null) {
				key.reset();
			}
		}
	} finally {
		watchService.close();
	}
	okForCleanup();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:47,代码来源:DirectoryWatcherTest.java

示例7: testEntryModify

import java.nio.file.WatchService; //导入方法依赖的package包/类
public void testEntryModify() throws IOException, InterruptedException, StfException {
	Path tempDirectory = getTemporaryDirectory();
	WatchService watchService = FileSystems.getDefault().newWatchService();
	try {
		Path newFilename = Paths.get("ENTRY_MODIFY.txt");
		Path newFile = tempDirectory.resolve(newFilename);
		Files.createFile(newFile);
		assertTrue("File was not created", Files.exists(newFile));

		// We need to pause so that the implementation on AIX and zOS
		// notices the modification time
		// has changed, which only work to the second apparently.
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {

		}

		tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);

		// Modify file
		OutputStream out = Files.newOutputStream(newFile, StandardOpenOption.WRITE);
		out.write("A".getBytes());
		out.close();

		WatchKey key = null;

		// We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got
		// something by
		// then we assume failure

		key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);

		assertNotNull("Polling WatchService object returned null", key);

		boolean eventFound = false;

		// Check for exactly one event
		for (WatchEvent<?> event : key.pollEvents()) {
			if (event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
				// Assert exactly one event
				assertFalse("Duplicate ENTRY_MODIFY events delivered for one file modification", eventFound);
				// Assert filename is correct
				assertEquals(newFilename, (Path) event.context());
				eventFound = true;
			} else {
				fail(event.kind() + " event retured, expected ENTRY_MODIFY");
			}
		}

		// Reset the key, to allow for future events
		key.reset();
	} finally {
		watchService.close();
	}
	okForCleanup();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:58,代码来源:DirectoryWatcherTest.java

示例8: testEntryDelete

import java.nio.file.WatchService; //导入方法依赖的package包/类
public void testEntryDelete() throws IOException, InterruptedException, StfException {
	Path tempDirectory = getTemporaryDirectory();
	WatchService watchService = FileSystems.getDefault().newWatchService();
	try {
		// Create the file before registering the WatchService, so that
		// we don't need to pause before deleting the file
		Path newFilename = Paths.get("ENTRY_DELETE.txt");
		Path newFile = tempDirectory.resolve(newFilename);
		Files.createFile(newFile);
		assertTrue("File was not created", Files.exists(newFile));

		tempDirectory.register(watchService, StandardWatchEventKinds.ENTRY_DELETE);

		Files.delete(newFile);

		WatchKey key = null;
		// We will give it POLL_TIMEOUT_SECONDS seconds, if it hasn't got
		// something by
		// then we assume failure

		key = watchService.poll(POLL_TIMEOUT_SECONDS, TimeUnit.SECONDS);

		assertNotNull("Polling WatchService object returned null", key);

		boolean eventFound = false;

		// Check for exactly one event
		for (WatchEvent<?> event : key.pollEvents()) {
			if (event.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)) {
				// Assert exactly one event
				assertFalse("Duplicate ENTRY_DELETE events delivered for one file deletion", eventFound);
				// Assert filename is correct
				assertEquals(newFilename, (Path) event.context());
				eventFound = true;
			} else {
				fail(event.kind() + " event retured, expected ENTRY_DELETE");
			}
		}
		// Reset the key, to allow for future events
		key.reset();
	} finally {
		watchService.close();
	}
	okForCleanup();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:46,代码来源:DirectoryWatcherTest.java

示例9: autoDeepScan

import java.nio.file.WatchService; //导入方法依赖的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;
		}
	}
}
 
开发者ID:zylo117,项目名称:SpotSpotter,代码行数:73,代码来源:FileListener.java

示例10: poll

import java.nio.file.WatchService; //导入方法依赖的package包/类
private WatchKey poll(WatchService service) throws InterruptedException {
    return service.poll(10, TimeUnit.SECONDS);
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:4,代码来源:WatchServiceTest.java


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