title: Exponential Backoff Algorithm with Full Jitter Java Implementation
body: <p>I am trying to create a utility class in pure Java that will contain the logic needed for the Exponential Backoff Algorithm implementation with full jitter as there will be multiple clients sending requests.
  I have another class with a method that executes a GET or a POST request and returns a response with a status code. I want to retry (aka use the exponential backoff strategy) only if the status code is in the 5xx. Current code is not compiling.</p>
  <p>The calling method looks like this:</p>
  <pre><code>HttpResponse response = executeGetRequest( params );
  int statusCode = response.getStatusCode();
  //some status code validation
  </code></pre>
  <p>My ExponentialBackoffStrategy class is:</p>
  <pre><code>public class ExponentialBackoffStrategy {
  
  private final long maxBackoff;
  
  private long backoffValue;
  
  private long attempts;
  private static final long DEFAULT_MAX_RETRIES = 900_000;
  
  private Random random = new Random();
  
  public ExponentialBackoffStrategy( long maxBackoff ) {
      this.maxBackoff = maxBackoff;
  }
  
  public long getWaitTimeExp() {
      if( backoffValue &gt;= maxBackoff ) {
          return maxBackoff;
      }
      double pow = Math.pow( 2, attempts++ );
      int rand = random.nextInt( 1000 );
      backoffValue = ( long ) Math.min( pow + rand, maxBackoff );
      return backoffValue;
  }
  
  public static ExponentialBackoffStrategy getDefault() {
      return new ExponentialBackoffStrategy( DEFAULT_MAX_RETRIES );
      }
  }
  </code></pre>
  <p>I want to get some feedback on the implemented class in regards to if I can do anything better and also how to integrate it with the caller method. My idea as of right now is:</p>
  <pre><code>ExponentialBackoffStrategy backoff = ExponentialBackoffStrategy.getDefault();
  boolean retry = false;
  HttpResponse response = null;
  int statusCode = 0;
  do {
    response = executeGetRequest( params );
    statusCode = response.getStatusLine().getStatusCode();
    if( statusCode &gt;= 500 &amp;&amp; statusCode &lt; 600 ) {
      retry = true;
      try {
          Thread.sleep( backoff.getWaitTimeExp() );
      } catch ( InterruptedException e ) {
          //handle exception
      }
    }
  } while ( retry );
  </code></pre>
  <p>Any help would be greatly appreciated!</p>
  <p><strong>EDIT:</strong>
  The response is actually located in try with resources.</p>
  <pre><code>try ( HttpResponse response = backoff.attempt(
  () -&gt; executeGetRequest( params ),
  r -&gt; {
    final int statusCode = response.getStatusLine().getStatusCode();
    return statusCode &lt; 500 || statusCode &gt;= 600;
  }
  );)
  </code></pre>
  <p>I am running into two issues:</p>
  <ol>
  <li>On the line
  <code>final int statusCode = response.getStatusLine().getStatusCode();</code>
  &quot;response&quot; is underlined red &quot;variable 'response' may not have been initialized&quot;. Tried to take it outside the try block and try with resources doesn't like it.</li>
  <li>executeGetRequest now needs a catch block inside of the lambda:
  <code>try ( HttpResponse response = executePostRequest( params ) ) {</code></li>
  </ol>