View Javadoc

1   package org.kuali.common.util.wait;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.kuali.common.util.Assert;
5   import org.kuali.common.util.FormatUtils;
6   import org.kuali.common.util.ThreadUtils;
7   import org.kuali.common.util.condition.Condition;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  public class DefaultWaitService implements WaitService {
12  
13  	private static final Logger logger = LoggerFactory.getLogger(DefaultWaitService.class);
14  
15  	@Override
16  	public WaitResult wait(WaitContext context, Condition condition) {
17  		long start = System.currentTimeMillis();
18  		long timeout = start + context.getTimeoutMillis();
19  		ThreadUtils.sleep(context.getInitialPauseMillis());
20  		while (!condition.isTrue()) {
21  			long now = System.currentTimeMillis();
22  			Assert.isTrue(now <= timeout, "Timed out waiting");
23  			String elapsed = StringUtils.leftPad(FormatUtils.getTime(now - start), 7, " ");
24  			String timeoutString = StringUtils.leftPad(FormatUtils.getTime(timeout - now), 7, " ");
25  			logger.info("[elapsed: {}  timeout: {}]", elapsed, timeoutString);
26  			ThreadUtils.sleep(context.getSleepMillis());
27  		}
28  		return new WaitResult.Builder(start, System.currentTimeMillis()).build();
29  	}
30  
31  }