1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.wait;
17
18 import static com.google.common.base.Preconditions.checkState;
19 import static java.lang.System.currentTimeMillis;
20 import static org.apache.commons.lang3.StringUtils.leftPad;
21 import static org.kuali.common.util.base.Threads.sleep;
22 import static org.kuali.common.util.log.Loggers.newLogger;
23
24 import org.kuali.common.util.FormatUtils;
25 import org.kuali.common.util.condition.Condition;
26 import org.slf4j.Logger;
27
28 public class DefaultWaitService implements WaitService {
29
30 private static final Logger logger = newLogger();
31
32 @Override
33 public WaitResult wait(WaitContext context, Condition condition) {
34 long start = currentTimeMillis();
35 long timeout = start + context.getTimeoutMillis();
36 sleep(context.getInitialPauseMillis());
37 while (!condition.isTrue()) {
38 long now = currentTimeMillis();
39 checkState(now <= timeout, "Timed out waiting");
40 String elapsed = leftPad(FormatUtils.getTime(now - start), 7, " ");
41 String timeoutString = leftPad(FormatUtils.getTime(timeout - now), 7, " ");
42 logger.info("[elapsed: {} timeout: {}]", elapsed, timeoutString);
43 sleep(context.getSleepMillis());
44 }
45 return WaitResult.create(start, currentTimeMillis());
46 }
47
48 }