當前位置: 首頁>>代碼示例>>Java>>正文


Java ClosedWatchServiceException類代碼示例

本文整理匯總了Java中java.nio.file.ClosedWatchServiceException的典型用法代碼示例。如果您正苦於以下問題:Java ClosedWatchServiceException類的具體用法?Java ClosedWatchServiceException怎麽用?Java ClosedWatchServiceException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ClosedWatchServiceException類屬於java.nio.file包,在下文中一共展示了ClosedWatchServiceException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: nextEvent

import java.nio.file.ClosedWatchServiceException; //導入依賴的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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:23,代碼來源:NioNotifier.java

示例2: pollForMoreChanges

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
/**
 * Keep polling for a short time: when (multiple) directories get deleted the watch keys might
 * arrive just a bit later
 */
private void pollForMoreChanges() throws ClosedWatchServiceException, InterruptedException {
    boolean keepPolling = true;
    List<WatchKey> polledKeys = new ArrayList<>();
    final long startPolling = System.currentTimeMillis();
    while (keepPolling) {
        log.debug("Waiting {} ms for more changes...", POLLING_TIME_MILLIS);
        WatchKey key = watcher.poll(POLLING_TIME_MILLIS, TimeUnit.MILLISECONDS);
        if (key == null) {
            keepPolling = false;
        } else {
            log.debug("Found change for '{}' found during extra polling time", key.watchable());
            polledKeys.add(key);
        }
    }
    log.debug("Polled '{}' more changes during '{}' ms", polledKeys.size(), String.valueOf(System.currentTimeMillis() - startPolling));
    for (WatchKey polledKey : polledKeys) {
        processWatchKey(polledKey);
    }
}
 
開發者ID:openweb-nl,項目名稱:hippo-groovy-updater,代碼行數:24,代碼來源:FileSystemWatcher.java

示例3: poll

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(WatchService watcher) {
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:LotsOfCancels.java

示例4: poll

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(int id, WatchService watcher) {
    System.out.printf("begin poll %d%n", id);
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do but print
        System.out.printf("poll %d expected exception %s%n", id, expected);
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
    System.out.printf("end poll %d%n", id);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:LotsOfCancels.java

示例5: testWatchServiceTakeBlocks

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
@Test
@Category( { SlowTest.class, Watchable.class, Writable.class } )
@SuppressWarnings( "PMD.EmptyCatchBlock" )
public void testWatchServiceTakeBlocks() throws Exception {
    Path dir = dirTA();
    final WatchService watcher = dir.getFileSystem().newWatchService();
    dir.register( watcher, ENTRY_CREATE );

    final Ref<Boolean> interrupted = Ref.valueOf( false );

    new Thread( () -> {
        try {
            watcher.take();
        } catch( InterruptedException | ClosedWatchServiceException e ) {
            // nothing to do
        } finally {
            interrupted.set( true );

        }
    } ).start();

    Thread.sleep( 1000 );

    assertThat( interrupted.get() ).isFalse();

}
 
開發者ID:openCage,項目名稱:niotest,代碼行數:27,代碼來源:Tests11Watcher.java

示例6: handleChanges

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
private synchronized void handleChanges() {
    final WatchKey key;
    try {
        key = this.watchService.take();
    } catch (final InterruptedException | ClosedWatchServiceException e) {
        if (!ConfigLoaderImpl.this.closed) {
            LOG.warn(INTERRUPTED, e);
            Thread.currentThread().interrupt();
        }
        return;
    }

    if (key != null) {
        for (final WatchEvent<?> event : key.pollEvents()) {
            handleEvent(event.context().toString());
        }
        final boolean reset = key.reset();
        if (!reset) {
            LOG.warn("Could not reset the watch key.");
        }
    }
}
 
開發者ID:opendaylight,項目名稱:bgpcep,代碼行數:23,代碼來源:ConfigLoaderImpl.java

示例7: Watcher

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
public Watcher(final BiConsumer<Kind<?>, Path> listener, final Path... dirs)
    throws IOException {
  this.watcher = FileSystems.getDefault().newWatchService();
  this.keys = new HashMap<WatchKey, Path>();
  this.listener = listener;
  for (Path dir : dirs) {
    registerAll(dir);
  }

  this.scanner = new Thread(() -> {
    boolean process = true;
    listener.accept(ENTRY_MODIFY, dirs[0]);
    try {
    while (process) {
      process = processEvents();
    }
    } catch (ClosedWatchServiceException ex) {
      log.trace("watch service closed", ex);
    }
  }, "asset-compiler");

  scanner.setDaemon(true);
}
 
開發者ID:jooby-project,項目名稱:jooby,代碼行數:24,代碼來源:Watcher.java

示例8: testNewSingleThreadExecutor

import java.nio.file.ClosedWatchServiceException; //導入依賴的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
            }
        }
    }
}
 
開發者ID:sworisbreathing,項目名稱:sfmf4j,代碼行數:24,代碼來源:FileSystemFactoryTest.java

示例9: poll

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
@Override
public WatchKey poll() {
    if (!running) {
        throw new ClosedWatchServiceException();
    }
    WatchKey pending = popPending();
    if (null != pending) {
        return pending;
    }
    for (GlusterWatchKey k : paths) {
        if (k.isValid() && k.isReady() && k.update()) {
            pendingPaths.add(k);
        }
    }
    return popPending();
}
 
開發者ID:gluster,項目名稱:glusterfs-java-filesystem,代碼行數:17,代碼來源:GlusterWatchService.java

示例10: pumpEvents

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
private void pumpEvents() throws InterruptedException {
    while (isRunning()) {
        try {
            List<FileWatcherEvent> events = poller.takeEvents();
            if (events != null) {
                deliverEvents(events);
            }
        } catch (ClosedWatchServiceException e) {
            LOGGER.debug("Received ClosedWatchServiceException, stopping");
            stop();
        }
    }
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:14,代碼來源:WatchServiceFileWatcherBacking.java

示例11: addWatch

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
@Override
protected WatchKey addWatch(String pathStr) throws IOException {
    Path path = Paths.get(pathStr);
    try {
        WatchKey key = path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        return key;
    } catch (ClosedWatchServiceException ex) {
        throw new IOException(ex);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:11,代碼來源:NioNotifier.java

示例12: removeWatch

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
@Override
protected void removeWatch(WatchKey key) throws IOException {
    try {
        key.cancel();
    } catch (ClosedWatchServiceException ex) {
        throw new IOException(ex);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:9,代碼來源:NioNotifier.java

示例13: scanner

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
protected void scanner ()
{
    logger.trace ( "Watching for events" );
    while ( true )
    {
        WatchKey key = null;
        try
        {
            key = this.ws.take ();
            logger.trace ( "Took events: {}", key.watchable () );

            final List<WatchEvent<?>> events = key.pollEvents ();
            for ( final WatchEvent<?> evt : events )
            {
                processEvent ( evt );
            }
        }
        catch ( final InterruptedException | ClosedWatchServiceException e )
        {
            return;
        }
        finally
        {
            if ( key != null )
            {
                key.reset ();
            }
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:31,代碼來源:AbstractMergeWatcher.java

示例14: run

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
@Override
public void run() {
    try {
        log.info("Watch started");
        while (true) {
            processChanges();
        }
    } catch (ClosedWatchServiceException e) {
        log.info("Watch closed", e);
    } finally {
        IOUtils.closeQuietly(watcher);
    }
}
 
開發者ID:openweb-nl,項目名稱:hippo-groovy-updater,代碼行數:14,代碼來源:FileSystemWatcher.java

示例15: doPrivilegedRegister

import java.nio.file.ClosedWatchServiceException; //導入依賴的package包/類
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             int sensitivityInSeconds)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensitivityInSeconds);
        return watchKey;
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:37,代碼來源:PollingWatchService.java


注:本文中的java.nio.file.ClosedWatchServiceException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。