public void runWithRetries(RetryableTask task) throws Exception {
  boolean maxSleepIntervalHit=false;
  for (int i=0; i < numAttempts; i++) {
    try {
      task.run();
      return;
    }
 catch (    Exception e) {
      if (i == numAttempts - 1) {
        throw e;
      }
      int sleepTime;
      if (maxSleepIntervalHit) {
        sleepTime=maxSleepInterval;
      }
 else {
        sleepTime=baseSleepInterval * (int)Math.pow(2,i);
      }
      LOG.error("Got an exception! Sleeping for " + sleepTime + " seconds and retrying.",e);
      try {
        Thread.sleep(sleepTime * 1000);
      }
 catch (      InterruptedException ie) {
        LOG.error("Unexpected interruption!",ie);
        throw ie;
      }
    }
  }
}

public void runWithRetries ( RetryableTask task ) throws Exception { boolean maxSleepIntervalHit = false ; for ( int i = 0 ; i < numAttempts ; i ++ ) { try { task . run ( ) ; return ; } catch ( Exception e ) { if ( i == numAttempts - 1 ) { throw e ; } int sleepTime ; if ( maxSleepIntervalHit ) { sleepTime = maxSleepInterval ; } else { sleepTime = baseSleepInterval * ( int ) Math . pow ( 2 , i ) ; } LOG . error ( "Got an exception! Sleeping for " + sleepTime + " seconds and retrying." , e ) ; try { Thread . sleep ( sleepTime * 1000 ) ; } catch ( InterruptedException ie ) { LOG . error ( "Unexpected interruption!" , ie ) ; throw ie ; } } } }
run a task , retrying a fixed number of times if there is a failure .
