Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.amazonaws.services.simpleworkflow.flow.interceptors;
  
  import java.util.Date;
  

Author(s):
fateev
 
 public class AsyncRetryingExecutor implements AsyncExecutor {
 
     private final RetryPolicy retryPolicy;
 
     private WorkflowClock clock;
 
     public AsyncRetryingExecutor(RetryPolicy retryPolicyWorkflowClock clock) {
         this. = retryPolicy;
         this. = clock;
     }
 
     @Override
     public void execute(AsyncRunnable commandthrows Throwable {
         scheduleWithRetry(commandnull, 1, .currentTimeMillis(), 0);
     }
 
     private void scheduleWithRetry(final AsyncRunnable commandfinal Throwable failurefinal int attempt,
             final long firstAttemptTimefinal long timeOfRecordedFailurethrows Throwable {
 
         long delay = -1;
         if (attempt > 1) {
             if (!.isRetryable(failure)) {
                 throw failure;
             }
 
             delay = .nextRetryDelaySeconds(new Date(firstAttemptTime), new Date(timeOfRecordedFailure), attempt);
 
             if (delay < 0) {
                 throw failure;
             }
         }
 
         if (delay > 0) {
             Promise<Voidtimer = .createTimer(delay);
             new Task(timer) {
 
                 @Override
                 protected void doExecute() throws Throwable {
                     invoke(commandattemptfirstAttemptTime);
                 }
             };
         }
         else {
             invoke(commandattemptfirstAttemptTime);
         }
 
     }
 
     private void invoke(final AsyncRunnable commandfinal int attemptfinal long firstAttemptTime) {
         final Settable<ThrowableshouldRetry = new Settable<Throwable>();
 
         new TryCatchFinally() {
 
             Throwable failureToRetry = null;
 
             @Override
             protected void doTry() throws Throwable {
                 command.run();
             }
 
             @Override
             protected void doCatch(Throwable failurethrows Throwable {
                 if (failure instanceof CancellationException) {
                     throw failure;
                 }
                  = failure;
             }
 
             @Override
             protected void doFinally() throws Throwable {
                 shouldRetry.set();
             }
         };
 
         new Task(shouldRetry) {
 
             @Override
             protected void doExecute() throws Throwable {
                 Throwable failure = shouldRetry.get();
                 if (failure != null) {
                     scheduleWithRetry(commandfailureattempt + 1, firstAttemptTime.currentTimeMillis());
                 }
             }
         };
    }
New to GrepCode? Check out our FAQ X