本文整理汇总了Java中name.pachler.nio.file.WatchService.poll方法的典型用法代码示例。如果您正苦于以下问题:Java WatchService.poll方法的具体用法?Java WatchService.poll怎么用?Java WatchService.poll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类name.pachler.nio.file.WatchService
的用法示例。
在下文中一共展示了WatchService.poll方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPollTimeout
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public void testPollTimeout() throws IOException, ClosedWatchServiceException, InterruptedException{
// test to check if the polling timeout works properly.
// The timeouts don't need to be exact (they can't be), but
// they should at least be reasonably close (we assume 10% delta here)
// to the specified timeout value.
WatchService watcher = null;
try {
watcher = Bootstrapper.newWatchService();
File dir = createTempDir("testPollTimeout");
Path path = Paths.get(dir.getPath());
WatchKey key = path.register(watcher, ENTRY_CREATE);
long timeout = 2500;
long startTime = System.currentTimeMillis();
watcher.poll(timeout, TimeUnit.MILLISECONDS);
long endTime = System.currentTimeMillis();
long elapsed = endTime - startTime;
assertEquals(timeout, elapsed, timeout*.1);
timeout = 2000;
startTime = System.currentTimeMillis();
watcher.poll(timeout/1000, TimeUnit.SECONDS);
endTime = System.currentTimeMillis();
elapsed = endTime - startTime;
assertEquals(timeout, elapsed, timeout*.1);
} finally {
if(watcher != null)
watcher.close();
}
}
示例2: testRegister
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testRegister() throws InterruptedException, IOException{
WatchService service = Bootstrapper.newWatchService();
File dir = createTempDir("watched-dir");
Path path = Bootstrapper.newPath(dir);
WatchKey key1 = path.register(service, StandardWatchEventKind.ENTRY_CREATE);
File file = new File(dir, "file");
file.createNewFile();
wait(200);
WatchKey resultKey1 = service.poll(200, TimeUnit.MILLISECONDS);
assertTrue(resultKey1 == key1);
List<WatchEvent<?>> list1 = resultKey1.pollEvents();
assertTrue(listContainsEvent(list1, ENTRY_CREATE, "file"));
assertTrue(resultKey1.reset());
WatchKey key2 = path.register(service, ENTRY_CREATE, ENTRY_DELETE);
assertTrue(key1 == key2);
file.delete();
WatchKey resultKey2 = service.poll(200, TimeUnit.MILLISECONDS);
assertTrue(resultKey2 == key2);
List<WatchEvent<?>> list2 = resultKey2.pollEvents();
assertTrue(listContainsEvent(list2, ENTRY_DELETE, "file"));
assertTrue(resultKey2.reset());
service.close();
}
示例3: testACCURATE
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testACCURATE() throws InterruptedException, IOException{
WatchService service = Bootstrapper.newWatchService();
File dir = createTempDir("watched-dir");
Path path = Bootstrapper.newPath(dir);
// ensure that selected platforms support ACCURATE
boolean platformSupportsACCURATE =
System.getProperty("os.name").contains("Windows")
|| System.getProperty("os.name").contains("Linux");
WatchKey key1;
try {
key1 = path.register(service, new WatchEvent.Kind[]{StandardWatchEventKind.ENTRY_CREATE}, ExtendedWatchEventModifier.ACCURATE);
}catch(UnsupportedOperationException uox){
// implementation reports that ACCURATE is not supported,
// so ensure that that's true:
assertFalse(platformSupportsACCURATE);
return;
}
File file = new File(dir, "file");
file.createNewFile();
file.delete();
wait(200);
WatchKey resultKey1 = service.poll(200, TimeUnit.MILLISECONDS);
List<WatchEvent<?>> list1 = resultKey1.pollEvents();
assertTrue(resultKey1 == key1);
assertTrue(listContainsEvent(list1, ENTRY_CREATE, "file"));
assertFalse(listContainsEvent(list1, ENTRY_DELETE, "file"));
assertTrue(resultKey1.reset());
service.close();
}
示例4: testInvalidationByCancel
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testInvalidationByCancel() throws IOException, InterruptedException{
// test for KEY_INVALID events in case the user manually cancels
// the watch key by calling WatchKey.cancel().
WatchService service = FileSystems.getDefault().newWatchService();
File dir = createTempDir("testInvalidationByCancel");
Path path = Paths.get(dir.toString());
WatchKey key = path.register(service, ENTRY_CREATE, KEY_INVALID);
// invalidation by user
key.cancel();
assertFalse(key.isValid());
// Because we're listening for KEY_INVALID events, the key still
// needs to be queued
WatchKey k = service.poll();
assertSame(key, k);
List<WatchEvent<?>> events = k.pollEvents();
assertTrue(events.size()==1);
assertTrue(listContainsEvent(events, KEY_INVALID, null));
service.close();
}
示例5: testInvalidationByDelete
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testInvalidationByDelete() throws IOException, InterruptedException{
// test for KEY_INVALID events in case the Path for which they
// key was registered is removed
WatchService service = FileSystems.getDefault().newWatchService();
File dir = createTempDir("testInvalidationByDelete");
Path path = Paths.get(dir.toString());
WatchKey key = path.register(service, ENTRY_CREATE, KEY_INVALID);
// invalidation by deleting the watched path
boolean deleted = dir.delete();
assertTrue(deleted); // if delete fails, we can't test
wait(500); // wait a bit so that the deletion can propagate
// (Windows appears to be specifically slow here...)
// Because we're listening for KEY_INVALID events, the key still
// should still be queued
WatchKey k = service.poll();
assertSame(key, k);
// key should now be invalid
assertFalse(key.isValid());
List<WatchEvent<?>> events = k.pollEvents();
assertTrue(events.size()==1);
assertTrue(listContainsEvent(events, KEY_INVALID, null));
service.close();
}
示例6: testOneTaker
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testOneTaker() throws IOException, InterruptedException{
WatchService watcher = Bootstrapper.newWatchService();
File dir = createTempDir("dir1");
Path path = Bootstrapper.newPath(dir);
WatchKey wk = path.register(watcher, StandardWatchEventKind.ENTRY_CREATE);
// wait for 200ms - should return no key
WatchKey key = watcher.poll(200, TimeUnit.MILLISECONDS);
assertNull(key);
File file1 = new File(dir, "file1");
file1.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
List<WatchEvent<?>> eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
WatchEvent event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("file1")));
assertEquals(event.kind(), ENTRY_CREATE);
key.reset();
File file2 = new File(dir, "file2");
file2.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("file2")));
assertEquals(event.kind(), ENTRY_CREATE);
key.reset();
// test file with single-character file name
File file3 = new File(dir, "3");
file3.createNewFile();
wait(200);
key = watcher.take();
assertNotNull(key);
eventList = key.pollEvents();
assertTrue(eventList.size() == 1);
event = eventList.get(0);
assertEquals(event.context(), Bootstrapper.newPath(new File("3")));
assertEquals(event.kind(), ENTRY_CREATE);
watcher.close();
}
示例7: testRecursiveWatch
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public void testRecursiveWatch() throws IOException, InterruptedException{
WatchService service = Bootstrapper.newWatchService();
// test non-recursive watch first: make sure that changes
// to files in a child directory are ignored
File dir = createTempDir("watched-dir");
Path path = Bootstrapper.newPath(dir);
File subdir = new File(dir, "subdir");
subdir.mkdir();
WatchKey key1 = path.register(service, StandardWatchEventKind.ENTRY_CREATE);
File file1 = new File(subdir, "file1");
file1.deleteOnExit();
file1.createNewFile();
// the file that has just been created must be ignored.
WatchKey result1 = service.poll(200, TimeUnit.MILLISECONDS);
assertNull(result1);
WatchKey key2;
try {
WatchEvent.Kind[] eventKinds = { StandardWatchEventKind.ENTRY_CREATE };
key2 = path.register(service, eventKinds, ExtendedWatchEventModifier.FILE_TREE);
} catch (UnsupportedOperationException uox){
// on platforms other than Windows, we expect an illegal argument
// exception, because only Windows currently implements the
// FILE_TREE modifier.
if(!System.getProperty("os.name").contains("Windows") || Bootstrapper.isForcePollingEnabled())
return;
throw uox;
}
File file2 = new File(subdir, "file2");
file2.deleteOnExit();
file2.createNewFile();
WatchKey result2 = service.poll(200, TimeUnit.MILLISECONDS);
assertTrue(result2 == key2);
service.close();
}
示例8: testReset
import name.pachler.nio.file.WatchService; //导入方法依赖的package包/类
@Test
public synchronized void testReset() throws IOException, InterruptedException{
WatchService service = FileSystems.getDefault().newWatchService();
File dir = createTempDir("testReset");
Path path = Paths.get(dir.toString());
WatchKey key = path.register(service, ENTRY_CREATE);
// this event should now be picked up by the WatchService
File file1 = new File(dir, "file1");
file1.createNewFile();
WatchKey k = service.poll(200, TimeUnit.MILLISECONDS);
assertTrue(k == key);
List<WatchEvent<?>> list = k.pollEvents();
assertTrue(listContainsEvent(list, ENTRY_CREATE, "file1"));
// THIS IS THE IMPORTANT PART:
// before we call reset, we wait a wee bit. Then we create another file,
// wait a wee bit more so that the event can be delivered to the key,
// and then call reset. poll() should now return the key immediately
// (because it has events queued up)
wait(200);
File file2 = new File(dir, "file2");
file2.createNewFile();
wait(200);
boolean resetResult = k.reset();
assertTrue(resetResult);
// poll should return the key now
k = service.poll();
assertTrue(k == key);
list = k.pollEvents();
assertTrue(listContainsEvent(list, ENTRY_CREATE, "file2"));
service.close();
}