本文整理汇总了Java中java.nio.file.WatchService.close方法的典型用法代码示例。如果您正苦于以下问题:Java WatchService.close方法的具体用法?Java WatchService.close怎么用?Java WatchService.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.WatchService
的用法示例。
在下文中一共展示了WatchService.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import java.nio.file.WatchService; //导入方法依赖的package包/类
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(Path dir, WatchService watcher) {
try {
try {
Path file = dir.resolve("anyfile");
for (int i=0; i<2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
watcher.close();
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
}
示例2: handle
import java.nio.file.WatchService; //导入方法依赖的package包/类
/**
* Stress the given WatchService, specifically the cancel method, in
* the given directory. Closes the WatchService when done.
*/
static void handle(int id, Path dir, WatchService watcher) {
System.out.printf("begin handle %d%n", id);
try {
try {
Path file = dir.resolve("anyfile");
for (int i=0; i<2000; i++) {
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE);
Files.createFile(file);
Files.delete(file);
key.cancel();
}
} finally {
System.out.printf("WatchService %d closing ...%n", id);
watcher.close();
System.out.printf("WatchService %d closed %n", id);
}
} catch (Exception e) {
e.printStackTrace();
failed = true;
}
System.out.printf("end handle %d%n", id);
}
示例3: main
import java.nio.file.WatchService; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Path path = Paths.get("/home/proberts/Desktop");
WatchService watchService = path.getFileSystem().newWatchService();
path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
while(true) {
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
printEvent(event);
}
if(!watchKey.reset()) {
watchKey.cancel();
watchService.close();
}
}
}
示例4: testNewSingleThreadExecutor
import java.nio.file.WatchService; //导入方法依赖的package包/类
/**
* Tests {@link FileSystemFactory#newWatchService()}.
*
* @throws ClosedWatchServiceException if the newly-created service is closed
* @throws IOException if the test fails
* @throws InterruptedException hopefully never
*/
@Test
public void testNewSingleThreadExecutor() throws ClosedWatchServiceException, IOException, InterruptedException {
WatchService service = new FileSystemFactory().newWatchService();
try {
assertNotNull(service);
assertNull(service.poll()); // verifies the service is not closed
} finally {
if (service != null) {
try {
service.close();
}catch(IOException ex) {
//trap
}
}
}
}
示例5: newCloserTask
import java.nio.file.WatchService; //导入方法依赖的package包/类
/**
* Returns a task that closes the given WatchService.
*/
static Callable<Void> newCloserTask(WatchService watcher) {
return () -> {
try {
watcher.close();
return null;
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
};
}
示例6: close
import java.nio.file.WatchService; //导入方法依赖的package包/类
/**
* This will stop the validator and free all resources.
* <br/>
* After calling this method the method
* {@link CertificateValidator#verifyTrustChain(List)} will report all
* certificates as invalid.
*/
@Override
public void close() throws IOException {
final WatchService watchService;
final Thread thread;
// get and reset
synchronized (this) {
logger.info("Closing default certificate validator");
watchService = this.watchService;
thread = this.thread;
this.watchService = null;
this.thread = null;
this.trustedCertificates.clear();
this.authorityCertificates.clear();
}
// dispose
if (watchService != null) {
watchService.close();
}
if (thread != null) {
Uninterruptibles.joinUninterruptibly(thread);
}
}
示例7: run
import java.nio.file.WatchService; //导入方法依赖的package包/类
@Override
public void run()
{
try
{
WatchService watchService = FileSystems.getDefault().newWatchService();
_directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
while (! _finish)
{
WatchKey watchKey = watchService.take();
if (watchKey != null)
{
for (WatchEvent<?> watchEvent : watchKey.pollEvents())
{
Kind<?> kind = watchEvent.kind();
Path context = _directoryPath.resolve((Path) watchEvent.context());
if (context.equals(_filePath) && ((kind == StandardWatchEventKinds.ENTRY_CREATE) || (kind == StandardWatchEventKinds.ENTRY_MODIFY)))
_dataProvider.produce(context.toFile());
}
if (! watchKey.reset())
_finish = true;
}
}
watchService.close();
}
catch (InterruptedException interruptedException)
{
}
catch (Throwable throwable)
{
logger.log(Level.WARNING, "Problem while watching file", throwable);
}
}
示例8: run
import java.nio.file.WatchService; //导入方法依赖的package包/类
@Override
public void run()
{
try
{
WatchService watchService = FileSystems.getDefault().newWatchService();
_directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
while (! _finish)
{
WatchKey watchKey = watchService.take();
if (watchKey != null)
{
for (WatchEvent<?> watchEvent : watchKey.pollEvents())
{
final Kind<?> kind = watchEvent.kind();
Path context = _directoryPath.resolve((Path) watchEvent.context());
if ((kind == StandardWatchEventKinds.ENTRY_CREATE) || (kind == StandardWatchEventKinds.ENTRY_MODIFY))
_dataProvider.produce(context.toFile());
}
if (! watchKey.reset())
_finish = true;
}
}
watchService.close();
}
catch (InterruptedException interruptedException)
{
}
catch (Throwable throwable)
{
logger.log(Level.WARNING, "Problem while watching directory", throwable);
}
}
开发者ID:arjuna-technologies,项目名称:FileSystem_DataBroker_PlugIn,代码行数:39,代码来源:DirectoryChangeDataSource.java
示例9: testSimple
import java.nio.file.WatchService; //导入方法依赖的package包/类
@Test
@Ignore
public void testSimple() throws IOException {
Path rootPath = Paths.get(clusterUri);
WatchService watcher = rootPath.getFileSystem().newWatchService();
rootPath.register(watcher,
new WatchEvent.Kind<?>[] { ENTRY_MODIFY });
watcher.close();
}
示例10: 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();
}
示例11: testRegisterOnClosedWatchService
import java.nio.file.WatchService; //导入方法依赖的package包/类
@Test( expected = ClosedWatchServiceException.class )
@Category( { Watchable.class, Writable.class } )
public void testRegisterOnClosedWatchService() throws IOException {
WatchService watcher = FS.newWatchService();
watcher.close();
dirTAB().register( watcher, ENTRY_CREATE );
}
示例12: testCloseAWatchServiceCancelsKeys
import java.nio.file.WatchService; //导入方法依赖的package包/类
@Test
@Category( { SlowTest.class, Watchable.class, Writable.class } )
public void testCloseAWatchServiceCancelsKeys() throws Exception {
Path dir = dirTA();
final WatchService watcher = dir.getFileSystem().newWatchService();
WatchKey key = dir.register( watcher, ENTRY_CREATE );
watcher.close();
waitForWatchService();
assertThat( key.isValid() ).isFalse();
}
示例13: 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();
}
示例14: 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();
}
示例15: 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();
}