public class EventingWatchService extends Object implements PathWatchService
WatchService
interface to a PathWatchService
.
Since the idea of a WatchService
for a FileSystem
is that it
maps onto the native event notification facility where available, this class
is considered to be an "eventing" implementation of the
PathWatchService
interface.
Example:
FileSystem fs = FileSystems.getDefault(); PathWatchService s = new EventingWatchService(fs.newWatchService()); s.register(fs.getPath("/home/luke"), StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); s.start(); for (;;) { WatchKey k = s.take(); ... }
Constructor and Description |
---|
EventingWatchService(WatchService service)
Constructs an instance on the specified watch service.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this watch service.
|
WatchKey |
poll()
Retrieves and removes the next watch key, or
null if none are
present. |
WatchKey |
poll(long timeout,
TimeUnit unit)
Retrieves and removes the next watch key, waiting if necessary up to the
specified wait time if none are yet present.
|
WatchKey |
register(Path dir,
WatchEvent.Kind<?>... kinds)
Registers the specified directory, using the specified
WatchEvent
kinds and no WatchEvent modifiers, with this watch service. |
WatchKey |
register(Path dir,
WatchEvent.Kind<?>[] kinds,
WatchEvent.Modifier... modifiers)
Registers the specified directory, using the specified
WatchEvent
kinds and modifiers, with this watch service. |
void |
start()
Starts this service.
|
WatchKey |
take()
Retrieves and removes next watch key, waiting if none are yet present.
|
public EventingWatchService(WatchService service)
service
- the watch service to be adaptedpublic void start()
PathWatchService
start
in interface PathWatchService
public WatchKey register(Path dir, WatchEvent.Kind<?>... kinds) throws IOException
PathWatchService
WatchEvent
kinds and no WatchEvent
modifiers, with this watch service.
This method is intended to be similar in behavior to
Path.register(WatchService, WatchEvent.Kind[])
.
register
in interface PathWatchService
dir
- directory to watch; must not be a symbolic linkkinds
- events to register forClosedWatchServiceException
- if this watch service is closedNotDirectoryException
- if dir
is not a directoryIOException
- if an I/O error occurspublic WatchKey register(Path dir, WatchEvent.Kind<?>[] kinds, WatchEvent.Modifier... modifiers) throws IOException
PathWatchService
WatchEvent
kinds and modifiers, with this watch service.
This method is intended to be similar in behavior to
Path.register(WatchService, WatchEvent.Kind[], WatchEvent.Modifier[])
.
register
in interface PathWatchService
dir
- directory to watch; must not be a symbolic linkkinds
- events to register formodifiers
- modifiers modifying how dir
is registeredClosedWatchServiceException
- if this watch service is closedNotDirectoryException
- if dir
is not a directoryIOException
- if an I/O error occurspublic void close() throws IOException
java.nio.file.WatchService
If a thread is currently blocked in the take
or poll
methods waiting for a key to be queued then
it immediately receives a ClosedWatchServiceException
. Any
valid keys associated with this watch service are invalidated
.
After a watch service is closed, any further attempt to invoke
operations upon it will throw ClosedWatchServiceException
.
If this watch service is already closed then invoking this method
has no effect.
close
in interface Closeable
close
in interface AutoCloseable
close
in interface WatchService
IOException
- if an I/O error occurspublic WatchKey poll()
java.nio.file.WatchService
null
if none are
present.poll
in interface WatchService
null
ClosedWatchServiceException
public WatchKey poll(long timeout, TimeUnit unit) throws InterruptedException
java.nio.file.WatchService
poll
in interface WatchService
timeout
- how to wait before giving up, in units of unitunit
- a TimeUnit
determining how to interpret the timeout
parameternull
ClosedWatchServiceException
InterruptedException
- if interrupted while waitingpublic WatchKey take() throws InterruptedException
java.nio.file.WatchService
take
in interface WatchService
ClosedWatchServiceException
InterruptedException
- if interrupted while waiting