public class Repeater
extends java.lang.Object
Closure
s / Callable
-
the first is executed, then the second. If the second closure returns false, the loop
is repeated; if true, it finishes. Further customization can be applied to set the period
between loops and place a maximum limit on how long the loop should run for.
It is configured in a fluent manner. For example, in Groovy:
Repeater.create("Wait until the Frobnitzer is ready")
.repeat {
status = frobnitzer.getStatus()
}
.until {
status == "Ready" || status == "Failed"
}
.limitIterationsTo(30)
.run()
Or in Java:
Repeater.create("Wait until the Frobnitzer is ready")
.until(new Callable<Boolean>() {
public Boolean call() {
String status = frobnitzer.getStatus()
return "Ready".equals(status) || "Failed".equals(status);
}})
.limitIterationsTo(30)
.run()
Modifier and Type | Field and Description |
---|---|
static Duration |
DEFAULT_REAL_QUICK_PERIOD
A small initial duration that something should wait between repeats,
e.g.
|
Constructor and Description |
---|
Repeater() |
Repeater(java.lang.String description)
Construct a new instance of Repeater.
|
Modifier and Type | Method and Description |
---|---|
Repeater |
backoff(Duration initialDelay,
double multiplier,
Duration finalDelay)
sets the
delayOnIteration(Function) function to be an exponential backoff as follows: |
Repeater |
backoffTo(Duration finalDelay)
convenience to start with a 10ms delay and exponentially back-off at a rate of 1.2
up to a max per-iteration delay as supplied here.
|
static Repeater |
create() |
static Repeater |
create(java.lang.String description) |
Repeater |
delayOnIteration(com.google.common.base.Function<? super java.lang.Integer,Duration> delayFunction)
sets a function which determines how long to delay on a given iteration between checks,
with 0 being mapped to the initial delay (after the initial check)
|
Repeater |
every(Duration duration)
Set how long to wait between loop iterations, as a constant function in
delayOnIteration |
Repeater |
every(groovy.time.Duration duration) |
Repeater |
every(long period,
java.util.concurrent.TimeUnit unit)
Set how long to wait between loop iterations.
|
java.lang.String |
getDescription() |
Duration |
getTimeLimit() |
Repeater |
limitIterationsTo(int iterationLimit)
Set the maximum number of iterations.
|
Repeater |
limitTimeTo(Duration duration)
Set the amount of time to wait for the condition.
|
Repeater |
limitTimeTo(long deadline,
java.util.concurrent.TimeUnit unit) |
Repeater |
repeat()
Deprecated.
since 0.7.0 this is no-op, as the repeater defaults to repeating nothing, simply remove the call,
using just
Repeater.until(...) . |
Repeater |
repeat(java.util.concurrent.Callable<?> body)
Sets the main body of the loop.
|
Repeater |
repeat(java.lang.Runnable body)
Sets the main body of the loop.
|
Repeater |
rethrowException()
If the exit condition check throws an exception, it will be recorded and the last exception will be thrown on failure.
|
Repeater |
rethrowExceptionImmediately()
If the repeated body or the exit condition check throws an exception, then propagate that exception immediately.
|
boolean |
run()
Run the loop.
|
ReferenceWithError<java.lang.Boolean> |
runKeepingError() |
void |
runRequiringTrue() |
Repeater |
suppressWarnings() |
Repeater |
until(java.util.concurrent.Callable<java.lang.Boolean> exitCondition)
Set code fragment that tests if the loop has completed.
|
<T> Repeater |
until(T target,
com.google.common.base.Predicate<T> exitCondition) |
public static final Duration DEFAULT_REAL_QUICK_PERIOD
backoffTo(Duration)
.
Chosen to be small enough that a user won't notice at all, but we're not going to be chewing up CPU while waiting.
public Repeater()
public Repeater(java.lang.String description)
description
- a description of the operation that will appear in debug logs.public static Repeater create()
public static Repeater create(java.lang.String description)
public Repeater repeat()
Repeater.until(...)
.until(Callable)
insteadpublic Repeater repeat(java.lang.Runnable body)
body
- a closure or other Runnable that is executed in the main body of the loop.public Repeater repeat(java.util.concurrent.Callable<?> body)
body
- a closure or other Callable that is executed in the main body of the loop.public Repeater every(long period, java.util.concurrent.TimeUnit unit)
period
- how long to wait between loop iterations.unit
- the unit of measurement of the period.public Repeater every(Duration duration)
delayOnIteration
public Repeater every(groovy.time.Duration duration)
public Repeater delayOnIteration(com.google.common.base.Function<? super java.lang.Integer,Duration> delayFunction)
public Repeater backoff(Duration initialDelay, double multiplier, @Nullable Duration finalDelay)
delayOnIteration(Function)
function to be an exponential backoff as follows:initialDelay
- the delay on the first iteration, after the initial checkmultiplier
- the rate at which to increase the loop delay, must be >= 1finalDelay
- an optional cap on the loop delaypublic Repeater backoffTo(Duration finalDelay)
public Repeater until(java.util.concurrent.Callable<java.lang.Boolean> exitCondition)
exitCondition
- a closure or other Callable that returns a boolean. If this code returns true then the
loop will stop executing.public <T> Repeater until(T target, com.google.common.base.Predicate<T> exitCondition)
public Repeater rethrowException()
public Repeater rethrowExceptionImmediately()
public Repeater suppressWarnings()
public Repeater limitIterationsTo(int iterationLimit)
iterationLimit
- the maximum number of iterations.public Repeater limitTimeTo(long deadline, java.util.concurrent.TimeUnit unit)
deadline
- the time that the loop should wait.unit
- the unit of measurement of the period.limitTimeTo(Duration)
public Repeater limitTimeTo(Duration duration)
public boolean run()
public void runRequiringTrue()
public ReferenceWithError<java.lang.Boolean> runKeepingError()
public java.lang.String getDescription()
public Duration getTimeLimit()