0

I'm making a service call with some input objects to a database. Few of the objects succeed and rest of them fail with a reason. I want to have an exponential retry strategy to trigger the same call with the failed objects.

Is there any framework i can use with a defined retry strategy in java

Example call

Output output = mDbService.persist(List<Objects> objects)

The output of the service call looks like

class Output {
    int successCount;
    List<FailedObject> failedObject;
}

// Each failedObject has a structure

class FailedObject {
   Object object;
   int errorCode; // Some are retryable and other are not.
}

samuel koduri
  • 389
  • 2
  • 4
  • 9

1 Answers1

0

I don’t know, a framework seems overkill, but maybe I’m misunderstanding your question.

Would something like this do what you want?

Output output = mDbService.persist(objects);
while(output.failedObject.size() > 0){
    List<Object> retryList = new ArrayList<>();
    for(FailedObject failed:output.failedObject){
        if(shouldRetry(failed.errorCode)){
            retryList.add(failed.object);
        }
    }
    output = mDbService.persist(retryList);
}

You will naturally also have to provide an implementation that checks the error code and performs the logic to determine if the associated object should be retried or not.

private boolean shouldRetry(int errorCode){
    // your logic here
}
Kirit
  • 305
  • 1
  • 3
  • 8
  • Of course this is the traditional way. But, i want to do an exponential retry strategy with a wait time before retrying the failed DSNs. can any framework like FailSafe as such help here? – samuel koduri Jan 09 '21 at 05:52