T
- type of data to aggregatepublic abstract class AbstractTimedAggregator<T> extends Object implements Aggregator<T>
evaluate
to represent an "aggregated" value over a
certain period of time. Note that you can still have a regular aggregator
extending this class by specifying an interval less than or equal to zero.
The regular flush/reset will be triggered from a timer which will always be
started as a daemon thread (so it will stop when there are no more non-daemon
threads in the JVM); this class allows 2 types of timers:
Timer
-- each instance of this class
will create a new Timer
and this Timer
will have a
single TimerTask
scheduled, which is the one that resets this
Aggregator
regularly and sends notifications. This way, when the
Aggregator
instance is destroyed, the Timer
goes as
well.Timer
instance -- this class will create a static
instance of Timer
which can be shared by other instances of this
class. While this is a bit more effective from a memory and thread management
point of view, it has the downside that if the TimerTask
's are
not managed properly this can create memory leaks. So if you decide to take
this route make sure when you are finished with this instance, to always stop
the timer at the end.
Synchronization: This class provides a thread safe framework so when
doAdd(Object)
, reset()
and evaluate()
is called,
access is synchronized via a read-write lock. evaluate()
is
considered a read operation and doAdd(Object)
and reset()
are considered write operations.
Modifier and Type | Field and Description |
---|---|
static long |
NO_TIMER
As per
timer javadoc, if the interval specified is zero or less
there will be no Timer created/assigned to this instance. |
static String |
TIMER_NAME
Name of the shared timer which will run all the TimerTasks resulted from
creating instances of this class which are set to used the shared timer.
|
Constructor and Description |
---|
AbstractTimedAggregator()
Default constructor -- creates an instance of this aggregator with no
Timer . |
AbstractTimedAggregator(long interval)
Creates an aggregator which has a timer at the specified interval
(miliseconds) and uses its own timer rather than the shared
MAIN_TIMER . |
AbstractTimedAggregator(long interval,
boolean useSharedTimer)
Creates an aggregator which has a timer at the specified interval and
also allows control over using the
shared timer or
its own per-instance timer. |
Modifier and Type | Method and Description |
---|---|
void |
add(T data)
Adds the data to this aggregator.
|
void |
addTimerListener(TimedAggregatorListener<T> listener)
If this
Aggregator has been started with timer support, it
will add the given listener, so it receives
timer events . |
protected abstract void |
doAdd(T data)
Function provided to allow subclasses to perform the actual adding of the
data to the aggregator.
|
protected abstract T |
doEvaluate()
Allows subclasses to perform the actual evaluation of the aggregated
result in a thread-safe manner.
|
protected abstract void |
doReset()
Function provided to allow subclasses to perform the actual reset of the
aggregator.
|
T |
evaluate()
Aggregates all the data this object has been "fed" via calls to
add(Object) . |
protected void |
finalize() |
int |
getDataSize()
Retrieves the size of the currently-stored data series.
|
long |
getInterval()
Getter for
interval . |
boolean |
isSharedTimer()
Checks whether this instance uses its own timer or
the
shared timer for scheduling the timer task . |
boolean |
isTimerEnabled()
Checks whether this instance has a timer associated with it or not.
|
boolean |
removeTimerListener(TimedAggregatorListener<T> listener)
Removes a listener from the timer listeners list if previously added.
|
void |
reset()
|
protected abstract int |
retrieveDataSize()
Function provided to allow subclasses to retrieve the actual size of the
data series.
|
void |
stop()
Cancels the current timer task (if set) -- which means from there on the
data will not be reset anymore.
|
String |
toString() |
public static final long NO_TIMER
timer
javadoc, if the interval specified is zero or less
there will be no Timer
created/assigned to this instance.
This constant is defined to make it easier to read code which creates
instances of this class and doesn't assign them a timer.public static final String TIMER_NAME
TIMER_NAME + hashCode()
.public AbstractTimedAggregator()
Timer
. Equivalent to
AbstractTimedAggregator(NO_TIMER)
.AbstractTimedAggregator(long)
public AbstractTimedAggregator(long interval)
MAIN_TIMER
. Equivalent to
AbstractTimedAggregator(interval,false)
.interval
- interval in miliseconds to set the timer for.interval
,
timer
,
AbstractTimedAggregator(long, boolean)
public AbstractTimedAggregator(long interval, boolean useSharedTimer)
shared timer
or
its own per-instance timer.interval
- interval in miliseconds to set the timer for.useSharedTimer
- if set to true
, timer
will be set to
TIMER_NAME
, otherwise a new instance of
Timer
will be created.public final long getInterval()
interval
.interval
.public final void add(T data)
dataLock
for writing then calls doAdd(Object)
, which
allows subclasses to perform the actual adding to the aggregator and then
at the end it unlocks dataLock
.add
in interface Aggregator<T>
data
- Data to be added to the aggregator.doAdd(Object)
,
dataLock
protected abstract void doAdd(T data)
add(Object)
so that access to any internal data series (implemented by subclasses)
via add(Object)
or evaluate()
or reset()
is
prohibited during this call, as a write lock is acquired prior to
this function call to ensure this function is the only one which has
access to the data.data
- Data to be aggregatedadd(Object)
public final T evaluate()
add(Object)
. Note that this object delegates the call to
doEvaluate()
after it secured read-only access to
dataLock
-- so any data series access can be safely read
(however, subclasses should NOT try to modify any data series they might
implement at this point!). The lock is released after
doEvaluate()
returns.evaluate
in interface NullaryFunction<T>
doEvaluate()
doEvaluate()
protected abstract T doEvaluate()
add(Object)
and
reset()
) is prohibited until this function finishes. However,
please note that other read access (via calls to the same
evaluate()
) is possible.public final void reset()
dataLock
for
writing then calls doReset()
, which allows subclasses to perform
the actual resetting of the aggregator and then at the end it unlocks
dataLock
.reset
in interface Aggregator<T>
doReset()
protected abstract void doReset()
reset()
so that access
to data (via add(Object)
or evaluate()
or
reset()
) is prohibited during this call, as a write lock
is acquired prior to this function call to ensure this function is the
only one which has access to the data.public final int getDataSize()
dataLock
for reading then calls
retrieveDataSize()
, which allows subclasses to compute the data
series size and then at the end it unlocks dataLock
.evaluate()
protected abstract int retrieveDataSize()
getDataSize()
so that
access to data (via add(Object)
or reset()
) is
prohibited during this call, as a read lock is acquired prior to
this function call. (However, calls to evaluate()
are allowed as
that locks for reading too.)public final void addTimerListener(TimedAggregatorListener<T> listener)
Aggregator
has been started with timer support, it
will add the given listener, so it receives
timer events
. If no timer support has been configured for this
Aggregator, this call has no effect.listener
- Listener to be added to received timer events from this
aggregator.timerListeners
public final boolean removeTimerListener(TimedAggregatorListener<T> listener)
false
.listener
- Listener to be removed from the list. NullPointerException
thrown if this is null.true
if this Aggregator has timer support and the
listener passed in was previously added (via
addTimerListener(TimedAggregatorListener)
) or false if
either the Aggregator has no timer support or it has timer
support but the listener was never registered with this
Aggregator.timerListeners
public final boolean isTimerEnabled()
task
member
should be set to a non-null value.true
if task
is not null,
false
otherwise (in which case there is no timer).public final boolean isSharedTimer()
the
shared timer
for scheduling the timer task
.true
if timer == MAIN_TIMER
or
false
otherwise.public final void stop()
timer
is not set to
the shared timer
then it will be cancelled as well
Also releases all the listeners from the list
.Copyright © 2003–2014 The Apache Software Foundation. All rights reserved.