public class MultipleRepeatGuard
extends java.lang.Object
RepeatGuard objects associated with String IDs.
For example, if your application repeatedly sends an event related to the unavailability of some component,
and you want to limit these events on a per-component basis, then you either need to keep and use a separate RepeatGuard object for every component instance,
or alternatively you can use one instance of MultipleRepeatGuard and take the component names as IDs.
The additional benefit is that this class optionally offers a bounded cache for the various RepeatGuards, which limits memory consumption for cases in which the number of possible IDs is large, e.g. if the ID is the log message coming from unknown / 3rd party source. The drawback is that the least accessed RepeatGuards will be removed internally when the cache is full and will be re-created upon access, which may distort the original guarding behavior (e.g. because the first check() of the re-created RepeatGuard always returns true, even if the old RepeatGuard object would have returned false for another 100 invocations, or because RepeatGuard.counterAtLastExecution() will not be accurate.
| Constructor and Description |
|---|
MultipleRepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
Constructor for a
MultipleRepeatGuard without a bounded RepeatGuard cache
(which means that memory limits indirectly set the bound, with possible side effects). |
MultipleRepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic,
int maxCacheSize)
Constructor for a
MultipleRepeatGuard with a bounded RepeatGuard cache. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
check(java.lang.String ID) |
boolean |
checkAndIncrement(java.lang.String ID) |
void |
clearCache() |
int |
counter(java.lang.String ID) |
int |
counterAtLastExecution(java.lang.String ID)
Note that depending on the RepeatGuard cache limit, the RepeatGuard for the given ID
may get re-created during this call, and thus the counter may be 0
even though the old "true" counter was > 0.
|
int |
getCacheSize()
Gets the actual number of RepeatGuards in the internal cache,
regardless of whether there is an upper limit and whether it has been reached etc.
|
RepeatGuard |
getRepeatGuard(java.lang.String ID) |
boolean |
hasRepeatGuard(java.lang.String ID) |
void |
increment(java.lang.String ID) |
void |
setCacheLimit(int maxEntries) |
void |
setRepeatGuard(java.lang.String ID,
RepeatGuard guard)
Sets a possibly modified
RepeatGuard for the given ID,
which allows for some RepeatGuards having different settings than those passed in
#MultipleRepeatGuard(long, TimeUnit, int, Logic, int). |
public MultipleRepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic,
int maxCacheSize)
MultipleRepeatGuard with a bounded RepeatGuard cache.
The first 4 parameters are the same as in RepeatGuard.RepeatGuard(long, TimeUnit, int, Logic)
and are used as defaults for all RepeatGuards created by this class.
To change these values for particular RepeatGuards, use setRepeatGuard(String, RepeatGuard)
using a new RepeatGuard object or the one obtained beforehand from getRepeatGuard(String).
interval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of repetitions.logic - Evaluation logic for interval and maxRepetitions.
The logic will be "reduced" automatically if interval or maxRepetitions
have a value <= 0, so as to be based only on the other positive value.maxCacheSize - Number of IDs (as used in check(String)) whose repetition we'll keep track of.
Setting this value higher will use more memory;
setting it lower than the actual number of IDs will result in unnecessary execution of the guarded actions,
as the matching RepeatGuards will be lost and re-created.java.lang.IllegalArgumentException - if maxRepetitions <= 0 && interval <= 0public MultipleRepeatGuard(long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions,
RepeatGuard.Logic logic)
MultipleRepeatGuard without a bounded RepeatGuard cache
(which means that memory limits indirectly set the bound, with possible side effects).#MultipleRepeatGuard(long, TimeUnit, int, Logic, int)public boolean check(java.lang.String ID)
RepeatGuard.check()public boolean checkAndIncrement(java.lang.String ID)
RepeatGuard.checkAndIncrement()public void increment(java.lang.String ID)
RepeatGuard.increment()public int counter(java.lang.String ID)
RepeatGuard.counter()public int counterAtLastExecution(java.lang.String ID)
RepeatGuard.counterAtLastExecution()public boolean hasRepeatGuard(java.lang.String ID)
public RepeatGuard getRepeatGuard(java.lang.String ID)
public void setRepeatGuard(java.lang.String ID,
RepeatGuard guard)
RepeatGuard for the given ID,
which allows for some RepeatGuards having different settings than those passed in
#MultipleRepeatGuard(long, TimeUnit, int, Logic, int).ID - guard - public int getCacheSize()
public void clearCache()
public void setCacheLimit(int maxEntries)