Brooklyn

brooklyn.util.internal
[Java] Class Repeater

java.lang.Object
  brooklyn.util.internal.Repeater

public class Repeater

Simple DSL to repeat a fragment of code periodically until a condition is satisfied. In its simplest case, it is passed two groovy.lang.Closures / java.util.concurrent.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() {
              public Boolean call() {
                  String status = frobnitzer.getStatus()
                  return "Ready".equals(status) || "Failed".equals(status);
              })
     .limitIterationsTo(30)
     .run()
 }
 


Constructor Summary
Repeater()

Repeater(java.util.Map flags)

Repeater(java.lang.String description)

Repeater(java.util.Map flags, java.lang.String description)

Construct a new instance of Repeater.

 
Method Summary
static Repeater create()

static Repeater create(java.util.Map flags)

static Repeater create(java.lang.String description)

static Repeater create(java.util.Map flags, java.lang.String description)

Repeater every(long period, java.util.concurrent.TimeUnit unit)

Set how long to wait between loop iterations.

Repeater every(groovy.time.Duration duration)

@see #every(long, TimeUnit)

Repeater every(long duration)

@depreated specify unit

See Also:
every(long, TimeUnit)

Repeater limitIterationsTo(int iterationLimit)

Set the maximum number of iterations.

Repeater limitTimeTo(long deadline, java.util.concurrent.TimeUnit unit)

Set the amount of time to wait for the condition.

Repeater limitTimeTo(groovy.time.Duration duration)

@see #limitTimeTo(long, TimeUnit)

Repeater repeat()

Set the main body of the loop.

Repeater repeat(java.lang.Runnable body)

Repeater repeat(java.util.concurrent.Callable body)

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.

void setFromFlags(java.util.Map flags)

Repeater suppressWarnings()

Repeater until(java.util.concurrent.Callable exitCondition)

Set code fragment that tests if the loop has completed.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Constructor Detail

Repeater

public Repeater()


Repeater

public Repeater(java.util.Map flags)


Repeater

public Repeater(java.lang.String description)


Repeater

public Repeater(java.util.Map flags, java.lang.String description)
Construct a new instance of Repeater.
Parameters:
flags: - period, timeout, description
description - a description of the operation that will appear in debug logs.


 
Method Detail

create

public static Repeater create()


create

public static Repeater create(java.util.Map flags)


create

public static Repeater create(java.lang.String description)


create

public static Repeater create(java.util.Map flags, java.lang.String description)


every

public Repeater every(long period, java.util.concurrent.TimeUnit unit)
Set how long to wait between loop iterations.
Parameters:
period - how long to wait between loop iterations.
unit - the unit of measurement of the period.
Returns:
{@literal this} to aid coding in a fluent style.


every

public Repeater every(groovy.time.Duration duration)
See Also:
every(long, TimeUnit)


every

public Repeater every(long duration)
depreated:
specify unit
See Also:
every(long, TimeUnit)


limitIterationsTo

public Repeater limitIterationsTo(int iterationLimit)
Set the maximum number of iterations. The loop will exit if the condition has not been satisfied after this number of iterations.
Parameters:
iterationLimit - the maximum number of iterations.
Returns:
{@literal this} to aid coding in a fluent style.


limitTimeTo

public Repeater limitTimeTo(long deadline, java.util.concurrent.TimeUnit unit)
Set the amount of time to wait for the condition. The repeater will wait at least this long for the condition to be true, and will exit soon after even if the condition is false.
Parameters:
deadline - the time that the loop should wait.
unit - the unit of measurement of the period.
Returns:
{@literal this} to aid coding in a fluent style.


limitTimeTo

public Repeater limitTimeTo(groovy.time.Duration duration)
See Also:
limitTimeTo(long, TimeUnit)


repeat

public Repeater repeat()
Set the main body of the loop.
Parameters:
body - a closure or other Runnable that is executed in the main body of the loop.
Returns:
{@literal this} to aid coding in a fluent style.


repeat

public Repeater repeat(java.lang.Runnable body)


repeat

public Repeater repeat(java.util.concurrent.Callable body)


rethrowException

public Repeater rethrowException()
If the exit condition check throws an exception, it will be recorded and the last exception will be thrown on failure.
Returns:
{@literal this} to aid coding in a fluent style.


rethrowExceptionImmediately

public Repeater rethrowExceptionImmediately()
If the repeated body or the exit condition check throws an exception, then propagate that exception immediately.
Returns:
{@literal this} to aid coding in a fluent style.


run

public boolean run()
Run the loop.
Returns:
true if the exit condition was satisfied; false if the loop terminated for any other reason.


setFromFlags

public void setFromFlags(java.util.Map flags)


suppressWarnings

public Repeater suppressWarnings()


until

public Repeater until(java.util.concurrent.Callable exitCondition)
Set code fragment that tests if the loop has completed.
Parameters:
exitCondition - a closure or other Callable that returns a boolean. If this code returns {@literal true} then the loop will stop executing.
Returns:
{@literal this} to aid coding in a fluent style.


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.