當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。