Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functionality for automatic retry after exception

I have made this abstract class to automatically retry network calls if some exception is thrown.

  • I take care to not retry after InterruptedException & UnknownHostException.
  • I retry 5 times. After each failure I perform an exponential back off, starting from 300ms going upto 1500ms.
public abstract class AutoRetry {

  private Object dataToReturn = null;
  public Object getDataToReturn() {
    return this.dataToReturn;
  }

  public AutoRetry() {

    short retry = -1;
    while (retry++ < StaticData.NETWORK_RETRY) {

      try {
        Thread.sleep(retry * StaticData.NETWORK_CALL_WAIT);
        this.dataToReturn = doWork();
        break;
      } catch (InterruptedException | UnknownHostException e) {
        e.printStackTrace();
        this.dataToReturn = null;
        return;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  protected abstract Object doWork() throws IOException;
}

I use it as follows :

final Object dataAfterWork = new AutoRetry() {     
  @Override
  protected Object doWork() throws IOException {
    return; //a network call which returns something
  }
}.getDataToReturn();

So is this implementation good/correct ?


EDIT

moved to https://codereview.stackexchange.com/questions/87686

like image 531
Dexter Avatar asked Apr 22 '15 13:04

Dexter


Video Answer


2 Answers

Have a look at Failsafe. It supports retry policies, synchronous and async retries, event listeners, integration with Java 8's CompletableFuture, etc. Ex:

RetryPolicy retryPolicy = new RetryPolicy()
  .retryOn(ConnectException.class)
  .withDelay(1, TimeUnit.SECONDS)
  .withMaxRetries(100);

// Synchronous get with retries
Connection connection = Failsafe.with(retryPolicy).get(() -> connect());

// Asynchronous get with retries
Failsafe.with(retryPolicy, executor)
  .get(() -> connect())
  .onSuccess(connection -> log.info("Connected to {}", connection))
  .onFailure(failure -> log.error("Connection attempts failed", failure));
like image 195
Jonathan Avatar answered Sep 29 '22 08:09

Jonathan


This looks pretty good, but I would split the running task from the retry. Also use generics, don't just throw Object about.

Use a Java 8 lambda and the return of the method:

public static <T> Optional<T> doWithRetry(final Supplier<T> t) {
    for (int retry = 0; retry <= StaticData.NETWORK_RETRY; ++retry) {
        try {
            Thread.sleep(retry * StaticData.NETWORK_CALL_WAIT);
            return Optional.of(t.get());
        } catch (InterruptedException | UnknownHostException e) {
            LOGGER.log(Level.SEVERE, "Call failed.", e);
            return Optional.empty();
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Call failed. Retry.", e);
        }
    }
    LOGGER.log(Level.SEVERE, "Call failed. Retries exceeded.");
    return Optional.empty();
}

Also, use a real logger, not printStackTrace...

Usage:

final String data = doWithRetry(() -> {
   //do stuff 
});

If your lambda needs to throw an exception, you'll need to define your own @FunctionalInterface:

@FunctionalInterface
interface StuffDoer<T> {
    T doStuff() throws Exception;
}

And use that in the method signature, you'll need to handle generic Exception.

Pre-Java 8 usage:

final String data = doWithRetry(new StuffDoer<T>() {
    @Override
    public T get() throws Exception {
        return null;
    }
});
like image 2
Boris the Spider Avatar answered Oct 23 '22 03:10

Boris the Spider