Java - File Change Notification (WatchService)
Java - File Change Notification (WatchService)
• Improve performance of applications that are forced to poll the file system today
• WatchService
– Watch registered objects for events and changes
– Makes use of native event facility where available
– Supports concurrent processing of events
• Path implements Watchable
– Register directory to get events when entries are created, deleted, or modified
WatchService API
• WatchKey represents registration of a watchable object with a WatchService.
// register
WatchKey key = file.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
// cancel registration
key.cancel();
WatchKey represents registration of a watchable object with a WatchService.
• WatchKey signalled and queued when event detected
• WatchService poll or take methods used to retrieve signalled key.
class WatchService {
WatchKey take();
WatchKey poll();
WatchKey poll(long timeout, TimeUnit unit);
WatchKey represents registration of a watchable object with a WatchService.
• WatchKey signalled and queued when event detected
• WatchService poll or take methods used to retrieve signalled keys
• Process events
• WatchKey reset method returns key to ready state
Comments
Post a Comment