View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }