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 theEFileNotification
with 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 long
DEFAULT_WATCH_RATE
The default watch rate is one minute.static long
MAX_WATCH_RATE
The maximum watch rate is one hour.static long
MIN_WATCH_RATE
The minimum watch rate is one half a second.static java.lang.String
TIMER_NAME
The 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 boolean
isFileWatcherRunning(java.io.File file)
Returnstrue
if the file watcher service is running for the given subject andfalse
otherwise.static EMessageKey
key(java.io.File file)
Returns theEFileWatcher
message key for the given file or directory.java.lang.String
name()
Returns absolute file name as the eBus object name.void
publishStatus(EFeedState fs, IEPublishFeed f)
Starts or stops a file watcher entry depending onupFlag
.void
shutdown()
Shuts down the file watcher thread by stopping the watch timer task, retracting the notification advertisement, and clearing the file watchers collection.static void
startFileWatcher(java.io.File file)
Creates the eBus file watcher service using thedefault watch rate
.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.void
startup()
Advertises the file watcher service to the local JVM only.static void
stopFileWatcher(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:
startup
in 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:
shutdown
in 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:
publishStatus
in 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)
Returnstrue
if the file watcher service is running for the given subject andfalse
otherwise.- Parameters:
file
- check if this file or directory has a watcher.- Returns:
true
if 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 key
for this watcher. Use the returned key tobuild
a 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
- Iffile
isnull
or ifrate
is <MIN_WATCH_RATE
or >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 theEFileNotification
advertisement 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 theEFileWatcher
message 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.
-
-