public boolean run()

in brooklyn-server/core/src/main/java/org/apache/brooklyn/util/core/internal/Repeater.java [293:365]


    public boolean run() {
        Preconditions.checkState(body != null, "repeat() method has not been called to set the body");
        Preconditions.checkState(exitCondition != null, "until() method has not been called to set the exit condition");
        Preconditions.checkState(period != null, "every() method has not been called to set the loop period time units");

        Throwable lastError = null;
        int iterations = 0;
        long endTime = -1;
        if (durationLimit != null) {
            endTime = System.currentTimeMillis() + durationLimit;
        }

        while (true) {
            iterations++;

            try {
                body.call();
            } catch (Exception e) {
                log.warn(description, e);
                if (rethrowExceptionImmediately) throw Exceptions.propagate(e);
            }

            boolean done = false;
            try {
                lastError = null;
                done = exitCondition.call();
            } catch (Exception e) {
                if (log.isDebugEnabled()) log.debug(description, e);
                lastError = e;
                if (rethrowExceptionImmediately) throw Exceptions.propagate(e);
            }
            if (done) {
                if (log.isDebugEnabled()) log.debug("{}: condition satisfied", description);
                return true;
            } else {
                if (log.isDebugEnabled()) {
                    String msg = String.format("%s: unsatisfied during iteration %s %s", description, iterations,
                            (iterationLimit > 0 ? "(max "+iterationLimit+" attempts)" : "") + 
                            (endTime > 0 ? "("+Time.makeTimeStringRounded(endTime - System.currentTimeMillis())+" remaining)" : ""));
                    if (iterations == 1) {
                        log.debug(msg);
                    } else {
                        log.trace(msg);
                    }
                }
            }

            if (iterationLimit > 0 && iterations == iterationLimit) {
                if (log.isDebugEnabled()) log.debug("{}: condition not satisfied and exceeded iteration limit", description);
                if (rethrowException && lastError != null) {
                    log.warn("{}: error caught checking condition (rethrowing): {}", description, lastError.getMessage());
                    throw Exceptions.propagate(lastError);
                }
                if (warnOnUnRethrownException && lastError != null)
                    log.warn("{}: error caught checking condition: {}", description, lastError.getMessage());
                return false;
            }

            if (endTime > 0) {
                if (System.currentTimeMillis() > endTime) {
                    if (log.isDebugEnabled()) log.debug("{}: condition not satisfied and deadline {} passed", 
                            description, Time.makeTimeStringRounded(endTime - System.currentTimeMillis()));
                    if (rethrowException && lastError != null) {
                        log.error("{}: error caught checking condition: {}", description, lastError.getMessage());
                        throw Exceptions.propagate(lastError);
                    }
                    return false;
                }
            }

            Time.sleep(period);
        }
    }