Path: blob/master/src/java.base/share/classes/java/nio/file/WatchService.java
41159 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.io.Closeable;28import java.io.IOException;29import java.util.concurrent.TimeUnit;3031/**32* A watch service that <em>watches</em> registered objects for changes and33* events. For example a file manager may use a watch service to monitor a34* directory for changes so that it can update its display of the list of files35* when files are created or deleted.36*37* <p> A {@link Watchable} object is registered with a watch service by invoking38* its {@link Watchable#register register} method, returning a {@link WatchKey}39* to represent the registration. When an event for an object is detected the40* key is <em>signalled</em>, and if not currently signalled, it is queued to41* the watch service so that it can be retrieved by consumers that invoke the42* {@link #poll() poll} or {@link #take() take} methods to retrieve keys43* and process events. Once the events have been processed the consumer44* invokes the key's {@link WatchKey#reset reset} method to reset the key which45* allows the key to be signalled and re-queued with further events.46*47* <p> Registration with a watch service is cancelled by invoking the key's48* {@link WatchKey#cancel cancel} method. A key that is queued at the time that49* it is cancelled remains in the queue until it is retrieved. Depending on the50* object, a key may be cancelled automatically. For example, suppose a51* directory is watched and the watch service detects that it has been deleted52* or its file system is no longer accessible. When a key is cancelled in this53* manner it is signalled and queued, if not currently signalled. To ensure54* that the consumer is notified the return value from the {@code reset}55* method indicates if the key is valid.56*57* <p> A watch service is safe for use by multiple concurrent consumers. To58* ensure that only one consumer processes the events for a particular object at59* any time then care should be taken to ensure that the key's {@code reset}60* method is only invoked after its events have been processed. The {@link61* #close close} method may be invoked at any time to close the service causing62* any threads waiting to retrieve keys, to throw {@code63* ClosedWatchServiceException}.64*65* <p> File systems may report events faster than they can be retrieved or66* processed and an implementation may impose an unspecified limit on the number67* of events that it may accumulate. Where an implementation <em>knowingly</em>68* discards events then it arranges for the key's {@link WatchKey#pollEvents69* pollEvents} method to return an element with an event type of {@link70* StandardWatchEventKinds#OVERFLOW OVERFLOW}. This event can be used by the71* consumer as a trigger to re-examine the state of the object.72*73* <p> When an event is reported to indicate that a file in a watched directory74* has been modified then there is no guarantee that the program (or programs)75* that have modified the file have completed. Care should be taken to coordinate76* access with other programs that may be updating the file.77* The {@link java.nio.channels.FileChannel FileChannel} class defines methods78* to lock regions of a file against access by other programs.79*80* <h2>Platform dependencies</h2>81*82* <p> The implementation that observes events from the file system is intended83* to map directly on to the native file event notification facility where84* available, or to use a primitive mechanism, such as polling, when a native85* facility is not available. Consequently, many of the details on how events86* are detected, their timeliness, and whether their ordering is preserved are87* highly implementation specific. For example, when a file in a watched88* directory is modified then it may result in a single {@link89* StandardWatchEventKinds#ENTRY_MODIFY ENTRY_MODIFY} event in some90* implementations but several events in other implementations. Short-lived91* files (meaning files that are deleted very quickly after they are created)92* may not be detected by primitive implementations that periodically poll the93* file system to detect changes.94*95* <p> If a watched file is not located on a local storage device then it is96* implementation specific if changes to the file can be detected. In particular,97* it is not required that changes to files carried out on remote systems be98* detected.99*100* @since 1.7101*102* @see FileSystem#newWatchService103*/104105public interface WatchService106extends Closeable107{108109/**110* Closes this watch service.111*112* <p> If a thread is currently blocked in the {@link #take take} or {@link113* #poll(long,TimeUnit) poll} methods waiting for a key to be queued then114* it immediately receives a {@link ClosedWatchServiceException}. Any115* valid keys associated with this watch service are {@link WatchKey#isValid116* invalidated}.117*118* <p> After a watch service is closed, any further attempt to invoke119* operations upon it will throw {@link ClosedWatchServiceException}.120* If this watch service is already closed then invoking this method121* has no effect.122*123* @throws IOException124* if an I/O error occurs125*/126@Override127void close() throws IOException;128129/**130* Retrieves and removes the next watch key, or {@code null} if none are131* present.132*133* @return the next watch key, or {@code null}134*135* @throws ClosedWatchServiceException136* if this watch service is closed137*/138WatchKey poll();139140/**141* Retrieves and removes the next watch key, waiting if necessary up to the142* specified wait time if none are yet present.143*144* @param timeout145* how to wait before giving up, in units of unit146* @param unit147* a {@code TimeUnit} determining how to interpret the timeout148* parameter149*150* @return the next watch key, or {@code null}151*152* @throws ClosedWatchServiceException153* if this watch service is closed, or it is closed while waiting154* for the next key155* @throws InterruptedException156* if interrupted while waiting157*/158WatchKey poll(long timeout, TimeUnit unit)159throws InterruptedException;160161/**162* Retrieves and removes next watch key, waiting if none are yet present.163*164* @return the next watch key165*166* @throws ClosedWatchServiceException167* if this watch service is closed, or it is closed while waiting168* for the next key169* @throws InterruptedException170* if interrupted while waiting171*/172WatchKey take() throws InterruptedException;173}174175176