本文整理汇总了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;
}
示例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);
}
}
示例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;
}
}
示例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);
}
示例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();
}
示例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.");
}
}
}
示例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);
}
示例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
}
}
}
}
示例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();
}
示例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();
}
}
}
示例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);
}
}
示例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);
}
}
示例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 ();
}
}
}
}
示例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);
}
}
示例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;
}
}