dimanche 1 mai 2022

Java Retry design Pattern using functional programming

I am new to java functional programming and looking if there is any fancy java 8 feature that I can use for retry logic without much changes to the existing methods in the class:

public class MyClass{
  public Pair<Integer, Integer> funA(int paramA, String paramB, int paramC);
  public Pair<Integer, String> funB(int paramA);
}

Is it possible to write a wrapper around this class for a generic retry logic that can take any function of this class as input and retry based on the pair key like:

public static T RetryWrapper(Function func)
  while(retryCounter < maxRetries){
    Pair< Integer, T > result = func.get();
    if(result.getKey() > 0)
      return result.getValue();
    retryCounter++;
}

Any code examples on how to implement and use this would be very helpful.

Aucun commentaire:

Enregistrer un commentaire