Package net.sf.eBusx.io
Class EFileWatcher
- java.lang.Object
-
- net.sf.eBusx.io.EFileWatcher
-
- All Implemented Interfaces:
EObject,EPublisher
public final class EFileWatcher extends java.lang.Object implements EPublisher
This class provides an eBus file watcher service, allowing asynchronous notification when a file or directory is created, modified or deleted. This service only supports JVM-local subscribers. This publisher advertises theEFileNotificationwith the file name as subject. The subject pattern is ".+" which supports any file name.The file watcher service starts its timer task with the first subscriber and stops the timer task when there are no more subscribers. This means the timer is running only when there are watched files.
- Author:
- Charles Rapp
-
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_WATCH_RATEThe default watch rate is one minute.static longMAX_WATCH_RATEThe maximum watch rate is one hour.static longMIN_WATCH_RATEThe minimum watch rate is one half a second.static java.lang.StringTIMER_NAMEThe thread name is "EFileWatcher".-
Fields inherited from interface net.sf.eBus.client.EObject
NAME_NOT_SET
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleanisFileWatcherRunning(java.io.File file)Returnstrueif the file watcher service is running for the given subject andfalseotherwise.static EMessageKeykey(java.io.File file)Returns theEFileWatchermessage key for the given file or directory.java.lang.Stringname()Returns absolute file name as the eBus object name.voidpublishStatus(EFeedState fs, IEPublishFeed f)Starts or stops a file watcher entry depending onupFlag.voidshutdown()Shuts down the file watcher thread by stopping the watch timer task, retracting the notification advertisement, and clearing the file watchers collection.static voidstartFileWatcher(java.io.File file)Creates the eBus file watcher service using thedefault watch rate.static EMessageKeystartFileWatcher(java.io.File file, long rate)Creates an eBus file watcher service for the specified file or directory and checking at a given watch rate.voidstartup()Advertises the file watcher service to the local JVM only.static voidstopFileWatcher(java.io.File file)Stops the watcher thread.
-
-
-
Field Detail
-
DEFAULT_WATCH_RATE
public static final long DEFAULT_WATCH_RATE
The default watch rate is one minute. Value is in milliseconds.- See Also:
- Constant Field Values
-
MIN_WATCH_RATE
public static final long MIN_WATCH_RATE
The minimum watch rate is one half a second. Value is in milliseconds.- See Also:
- Constant Field Values
-
MAX_WATCH_RATE
public static final long MAX_WATCH_RATE
The maximum watch rate is one hour. Value is in milliseconds.- See Also:
- Constant Field Values
-
TIMER_NAME
public static final java.lang.String TIMER_NAME
The thread name is "EFileWatcher".- See Also:
- Constant Field Values
-
-
Method Detail
-
name
public java.lang.String name()
Returns absolute file name as the eBus object name.
-
startup
public void startup()
Advertises the file watcher service to the local JVM only. The timer task is started with the first subscriber and stopped when there are no more subscribers.- Specified by:
startupin interfaceEObject- See Also:
EObject.shutdown()
-
shutdown
public void shutdown()
Shuts down the file watcher thread by stopping the watch timer task, retracting the notification advertisement, and clearing the file watchers collection.- Specified by:
shutdownin interfaceEObject- See Also:
EObject.startup()
-
publishStatus
public void publishStatus(EFeedState fs, IEPublishFeed f)
Starts or stops a file watcher entry depending onupFlag. The notification subject is the file name.- Specified by:
publishStatusin interfaceEPublisher- Parameters:
fs- the f state is either up or down.f- the f state applies to this publish f.
-
isFileWatcherRunning
public static boolean isFileWatcherRunning(java.io.File file)
Returnstrueif the file watcher service is running for the given subject andfalseotherwise.- Parameters:
file- check if this file or directory has a watcher.- Returns:
trueif the file watcher service is running.
-
startFileWatcher
public static void startFileWatcher(java.io.File file)
Creates the eBus file watcher service using thedefault watch rate.- Parameters:
file- watch this given file or directory.- Throws:
java.lang.IllegalStateException- if the file watcher service is already running.
-
startFileWatcher
public static EMessageKey startFileWatcher(java.io.File file, long rate)
Creates an eBus file watcher service for the specified file or directory and checking at a given watch rate. Returns themessage keyfor this watcher. Use the returned key tobuilda subscription to the newly created file watcher.- Parameters:
file- watch this given file or directory.rate- check for file updates at this millisecond rate.- Returns:
- the file watcher message key used to subscribe to this file watcher.
- Throws:
java.lang.IllegalArgumentException- Iffileisnullor ifrateis <MIN_WATCH_RATEor >MAX_WATCH_RATE.java.lang.IllegalStateException- if the file watcher service is already running but at a different watch rate.
-
stopFileWatcher
public static void stopFileWatcher(java.io.File file)
Stops the watcher thread. This retracts theEFileNotificationadvertisement and discards all subscribed file watchers.- Parameters:
file- the file or directory. Must be the same as passed tostartFileWatcher(File)- See Also:
startFileWatcher(File),startFileWatcher(File, long)
-
key
public static EMessageKey key(java.io.File file)
Returns theEFileWatchermessage key for the given file or directory.- Parameters:
file- generate the message key for this file.- Returns:
- file watcher message key for the given file.
-
-