All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anaptecs.jeaf.workload.proxy.HttpRequestRetryStrategyImpl Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2004 - 2021 anaptecs GmbH, Burgstr. 96, 72764 Reutlingen, Germany
 *
 * All rights reserved.
 */
package com.anaptecs.jeaf.workload.proxy;

import java.io.IOException;

import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.TimeValue;

import com.anaptecs.jeaf.xfun.api.XFun;

/**
 * Class implements a retry strategy for request with Apache HTTP Client.
 * 
 * @author JEAF Development Team
 */
public class HttpRequestRetryStrategyImpl implements HttpRequestRetryStrategy {
  /**
   * Maximum number of retries before a request really failed.
   */
  private final int maxRetries;

  /**
   * Wait time between before a requested is executed again (milliseconds).
   */
  private final int retryInterval;

  /**
   * Initialize object.
   * 
   * @param pMaxRetires Maximum number of retries that should be done.
   * @param pRetryInterval Time interval between retries.
   */
  public HttpRequestRetryStrategyImpl( int pMaxRetires, int pRetryInterval ) {
    maxRetries = pMaxRetires;
    retryInterval = pRetryInterval;
  }

  /**
   * Method returns configured maximum number of retries.
   * 
   * @return int Method returns maximum number of retires.
   */
  public int getMaxRetries( ) {
    return maxRetries;
  }

  /**
   * Method returns retry interval.
   * 
   * @return int Retry interval.
   */
  public int getRetryInterval( ) {
    return retryInterval;
  }

  /**
   * Method checks if the passed request should be executed again or not.
   * 
   * @param pRequest Request that might be executed again. The parameter must not be null.
   * @param pCount Number of times it was already tried to execute request.
   */
  @Override
  public boolean retryRequest( HttpRequest pRequest, IOException pException, int pCount, HttpContext pContext ) {
    boolean lTryAgain;
    if (pCount <= maxRetries) {
      URIAuthority lAuthority = pRequest.getAuthority();
      String lURL =
          pRequest.getScheme() + "://" + lAuthority.getHostName() + ":" + lAuthority.getPort() + pRequest.getPath();
      XFun.getTrace().warn("Retrying to send http request to " + lURL);
      lTryAgain = true;
    }
    else {
      lTryAgain = false;
    }

    return lTryAgain;
  }

  /**
   * Method checks if the request with the passed response should be executed again.
   * 
   * @return boolean The method always returns false.
   */
  @Override
  public boolean retryRequest( HttpResponse pResponse, int pExecCount, HttpContext pContext ) {
    // We do not retry in case that we received an HTTP response
    // TODO Allow retry in case of http 503
    return false;
  }

  /**
   * Method returns the retry interval in case that a http response was received.
   * 
   * @return {@link TimeValue} Retry interval. The method never returns null.
   */
  @Override
  public TimeValue getRetryInterval( HttpResponse pResponse, int pExecCount, HttpContext pContext ) {
    return TimeValue.ofMilliseconds(retryInterval);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy