public class Repeater
extends java.lang.Object
implements java.util.concurrent.Callable<java.lang.Boolean>
In its simplest case it is passed two Callable
objects, the operation
and the condition which are executed in that order. This execution is repeated
until the condition returns true
, when the loop 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, as well as other timing and delay properties.
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()
The
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. |
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.
|
java.lang.Boolean |
call() |
static Repeater |
create() |
static Repeater |
create(java.lang.String description) |
Repeater |
delayOnIteration(<any> 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)
Deprecated.
since 0.11.0; explicit groovy utilities/support will be deleted (instead use
every(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(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.
|
Repeater |
rethrowExceptionImmediately(<any> val) |
boolean |
run()
Run the loop.
|
ReferenceWithError<java.lang.Boolean> |
runKeepingError() |
void |
runRequiringTrue() |
Repeater |
suppressWarnings() |
Repeater |
threaded()
Use a new thread for every iteration of the loop.
|
Repeater |
threaded(java.util.concurrent.ExecutorService executor)
Use the passed in
executor to generate threads for every iteration
of the loop. |
<T> Repeater |
until(<any> supplier,
<any> exitCondition) |
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,
<any> 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(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 threaded()
public Repeater threaded(java.util.concurrent.ExecutorService executor)
executor
to generate threads for every iteration
of the loop. Because the executor is externally managed it will not be
shut down
by us when we finish.executor
- an externally managed ExecutorService
to use when creating threads.#threaded()}
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
@Deprecated public Repeater every(groovy.time.Duration duration)
every(Duration)
).public Repeater delayOnIteration(<any> delayFunction)
public Repeater backoff(Duration initialDelay, double multiplier, @Nullable Duration finalDelay)
#delayOnIteration(Function)
function to be an exponential backoff.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, <any> exitCondition)
public <T> Repeater until(<any> supplier, <any> exitCondition)
public Repeater rethrowException()
public Repeater rethrowExceptionImmediately()
public Repeater rethrowExceptionImmediately(<any> val)
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()
public java.lang.Boolean call() throws java.lang.Exception
call
in interface java.util.concurrent.Callable<java.lang.Boolean>
java.lang.Exception