View Javadoc

1   package com.atlassian.plugin.util;
2   
3   import static java.lang.Thread.sleep;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   import java.util.concurrent.TimeUnit;
9   
10  /**
11   * Utility methods for synchronising on asynchronous processes
12   */
13  public class WaitUntil
14  {
15      private static final Log log = LogFactory.getLog(WaitUntil.class);
16  
17      private WaitUntil()
18      {}
19  
20      /**
21       * Invokes the wait condition, trying every second for 10 seconds
22       *
23       * @param waitCondition The condition that determines when to stop waiting
24       * @return True if the condition returned true
25       */
26      public static boolean invoke(final WaitCondition waitCondition)
27      {
28          return invoke(waitCondition, 60);
29      }
30  
31      /**
32       * Invokes the wait condition, trying every second for the configured seconds
33       *
34       * @param waitCondition The condition that determines when to stop waiting
35       * @param tries The number of tries to attempt
36       * @return True if the condition returned true
37       */
38      public static boolean invoke(final WaitCondition waitCondition, final int tries)
39      {
40          final int secondMillis = 1000;
41          return invoke(waitCondition, tries * secondMillis, TimeUnit.MILLISECONDS, secondMillis);
42      }
43  
44      /**
45       * Invokes the wait condition, trying every second for the configured seconds
46       *
47       * @param waitCondition The condition that determines when to stop waiting
48       * @param time the amount of time to wait
49       * @param unit the time unit time is specified in
50       * @param retryInterval how often to re-check the condition (specified in the supplied TimeUnit)
51       * @return True if the condition returned true
52       */
53      public static boolean invoke(final WaitCondition waitCondition, final int time, final TimeUnit unit, final int retryInterval)
54      {
55          final Timeout timeout = Timeout.getMillisTimeout(time, unit);
56          boolean successful = false;
57          while (!timeout.isExpired())
58          {
59              if (waitCondition.isFinished())
60              {
61                  successful = true;
62                  break;
63              }
64  
65              if (log.isInfoEnabled())
66              {
67                  log.info(waitCondition.getWaitMessage() + ", " + timeout.getRemaining());
68              }
69              try
70              {
71                  sleep(unit.toMillis(retryInterval));
72              }
73              catch (final InterruptedException e)
74              {
75                  break;
76              }
77          }
78          return successful;
79      }
80  
81      /**
82       * The condition to determine when to stop waiting
83       */
84      public interface WaitCondition
85      {
86          /**
87           * If the condition has been finished
88           * @return True if finished and should stop waiting
89           */
90          boolean isFinished();
91  
92          /**
93           * Gets the wait message to log for each try
94           * @return The string to print describing why the code is waiting
95           */
96          String getWaitMessage();
97      }
98  }