lovprogramming
lovprogramming

Reputation: 653

How to implement exponential delay/sleep for try & catch

I am trying to write another decorator like the RetriableProcessorDecorator below (as a separate class) so that it does an exponential delay on retry. For example, if a message fails to be processed, we wait 1 second (should be configurable), then 2s, then 4s, then 8s, then 16s, etc. I would want to use thread instead of busy wait since it is less expensive. I wrote a new class RetriableProcessorExponentialDecorator that does this, but I'm not sure if this is the right approach to take.

RetriableProcessorDecorator.java:

@Slf4j
@Setter
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class RetriableProcessorDecorator implements.   
AbsMessageProcessorDecorator {
private final AbsMessageProcessor messageProcessor;
@Autowired
private AbsMessageActiveMQConfiguration configuration;

@Override
public void onMessage(AbsMessage message) throws Exception {
    int executionCounter = 0;
    final int maxRetries = this.configuration.getExceptionRetry() + 1;
    do {
        executionCounter++;
        try {
            this.messageProcessor.onMessage(message);
        } catch (RetriableException e) {
            log.info("Failed to process message. Retry #{}", executionCounter);
        } catch (Exception e) {
            // We don't retry on this, only RetriableException.
            throw e;
        }
    } while (executionCounter < maxRetries);
}
}

RetriableProcessorExponentialDecorator.java (new class I'm implementing):

public class RetriableProcessorExponentialDecorator implements AbsMessageProcessorDecorator {
  private final AbsMessageProcessor messageProcessor;
  @Autowired
  private AbsMessageActiveMQConfiguration configuration;

  @Override
  public void onMessage(AbsMessage message) throws Exception {
    int executionCounter = 0;
    int delayCounter = 1000;
    final int maxRetries = this.configuration.getExceptionRetry() + 1;
    do {
      executionCounter++;
      try {
        this.messageProcessor.onMessage(message);
      } catch (RetriableException e) {
        log.info("Failed to process message. Retry #{}", executionCounter);
        Thread.sleep(delayCounter);
        delayCounter = delayCounter * 2;
      } catch (Exception e) {
        // We don't retry on this, only RetriableException.
        throw e;
      }
    } while (executionCounter < maxRetries && delayCounter < Long.MAX_VALUE);
  }
}

Upvotes: 2

Views: 4903

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 35123

In general I think your approach is fine.

However, if you wanted to make it configurable (i.e. more useful) then you could do something like:

double multiplier = 2.0; // make this configurable
...
delayCounter = (long) (delayCounter * (Math.pow(multiplier, executionCounter)));
Thread.sleep(delayCounter);

You could also add a way to configure a maximum delay which might be handy for some use-cases, e.g.:

long maxDelay = 300000; // 5 minutes
...
if (delayCounter > maxDelay) {
   delayCounter = maxDelay;
}
Thread.sleep(delayCounter);

Upvotes: 3

Related Questions