List of usage examples for com.amazonaws.retry RetryUtils isThrottlingException
public static boolean isThrottlingException(AmazonServiceException exception)
From source file:org.finra.dm.dao.helper.AwsExceptionRetryAdvice.java
License:Apache License
/** * Invokes the method, catches following various exceptions and retries as needed: 1. Throttling exception. 2. 5xx exception. 3. Error codes defined in * configuration to be retried./*w ww .ja v a2s .c om*/ * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ public Object retryOnException(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class<?> targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); // Get the max delay in seconds. long maxTotalDelay = getMaxRetryDelaySecs() * 1000L; // Initialize a retry count to know the number of times we have retried calling the method. int retryCount = 0; // Get the minimum and maximum retry delay. long minAwsDelay = dmStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MIN_RETRY_DELAY_SECS) * 1000L; long maxAwsDelay = dmStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MAX_RETRY_DELAY_SECS) * 1000L; StopWatch totalElapsedTimeStopWatch = new StopWatch(); totalElapsedTimeStopWatch.start(); // Loop indefinitely. We will exit the loop if the method returns normally or a non-retryable exception is thrown. // If a retryable exception is thrown, the loop will exit after we have exceeded the maximum retry delay seconds. while (true) { try { // Proceed to the join point (i.e. call the method and let it return normally). return pjp.proceed(); } catch (AmazonServiceException ase) { // Retry if: // 1) Is throttling exception. // 2) Is 5xx exception. // 3) Is an error to be re-tried on as defined in configuration. if (RetryUtils.isThrottlingException(ase) || is5xxException(ase) || isRetryableException(ase)) { long totalElapsedTime = totalElapsedTimeStopWatch.getTime(); // It's a retryable exception. Check if we've retried for enough time. if (totalElapsedTime >= maxTotalDelay) { // We've retried for enough time so re-throw the original exception. LOGGER.warn("An exception occurred while calling " + methodName + ". The method has been retried for " + (totalElapsedTime / 1000.0f) + " second(s) and will not be retried anymore since it would exceed the maximum retry delay." + " The exception will now be thrown."); throw ase; } // Get the next delay. long nextSleepDelay = Math.min(((long) Math.pow(2, retryCount) * minAwsDelay), maxAwsDelay); long timeLeftToRetry = maxTotalDelay - totalElapsedTime; // If time left to try is less than next sleep delay, then use time left as next sleep delay to be retried the last time. if (timeLeftToRetry < nextSleepDelay) { nextSleepDelay = timeLeftToRetry; } // Log a warning so we're aware that we are retrying. LOGGER.warn("A retryable exception occurred while calling " + methodName + ". " + (totalElapsedTime / 1000.0f) + " second(s) have elapsed since initial exception and maximum retry delay is " + (maxTotalDelay / 1000.0f) + " second(s). Will retry in " + (nextSleepDelay / 1000.0f) + " second(s)."); // We can retry again so increment a counter to keep track of the number of times we retried and recalculate the total elapsed time. retryCount++; // Sleep for the next sleep delay. dmThreadHelper.sleep(nextSleepDelay); } else { // It's not a retryable exception (i.e. some other type of service exception) so just re-throw it. throw ase; } } } }
From source file:org.finra.herd.dao.helper.AwsExceptionRetryAdvice.java
License:Apache License
/** * Invokes the method, catches following various exceptions and retries as needed: 1. Throttling exception. 2. 5xx exception. 3. Error codes defined in * configuration to be retried./*from w w w . j a v a2 s. co m*/ * * @param pjp the join point. * * @return the return value of the method at the join point. * @throws Throwable if any errors were encountered. */ public Object retryOnException(ProceedingJoinPoint pjp) throws Throwable { // Get the method name being invoked. Class<?> targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); String methodName = targetClass.getName() + "." + targetMethodSignature.getName(); // Get the max delay in seconds. long maxTotalDelay = getMaxRetryDelaySecs() * 1000L; // Initialize a retry count to know the number of times we have retried calling the method. int retryCount = 0; // Get the minimum and maximum retry delay. long minAwsDelay = herdStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MIN_RETRY_DELAY_SECS) * 1000L; long maxAwsDelay = herdStringHelper .getConfigurationValueAsInteger(ConfigurationValue.AWS_MAX_RETRY_DELAY_SECS) * 1000L; StopWatch totalElapsedTimeStopWatch = new StopWatch(); totalElapsedTimeStopWatch.start(); // Loop indefinitely. We will exit the loop if the method returns normally or a non-retryable exception is thrown. // If a retryable exception is thrown, the loop will exit after we have exceeded the maximum retry delay seconds. while (true) { try { // Proceed to the join point (i.e. call the method and let it return normally). return pjp.proceed(); } catch (AmazonServiceException ase) { // Retry if: // 1) Is throttling exception. // 2) Is 5xx exception. // 3) Is an error to be re-tried on as defined in configuration. if (RetryUtils.isThrottlingException(ase) || is5xxException(ase) || isRetryableException(ase)) { long totalElapsedTime = totalElapsedTimeStopWatch.getTime(); // It's a retryable exception. Check if we've retried for enough time. if (totalElapsedTime >= maxTotalDelay) { // We've retried for enough time so re-throw the original exception. LOGGER.warn("An exception occurred while calling " + methodName + ". The method has been retried for " + (totalElapsedTime / 1000.0f) + " second(s) and will not be retried anymore since it would exceed the maximum retry delay." + " The exception will now be thrown."); throw ase; } // Get the next delay. long nextSleepDelay = Math.min(((long) Math.pow(2, retryCount) * minAwsDelay), maxAwsDelay); long timeLeftToRetry = maxTotalDelay - totalElapsedTime; // If time left to try is less than next sleep delay, then use time left as next sleep delay to be retried the last time. if (timeLeftToRetry < nextSleepDelay) { nextSleepDelay = timeLeftToRetry; } // Log a warning so we're aware that we are retrying. LOGGER.warn("A retryable exception occurred while calling " + methodName + ". " + (totalElapsedTime / 1000.0f) + " second(s) have elapsed since initial exception and maximum retry delay is " + (maxTotalDelay / 1000.0f) + " second(s). Will retry in " + (nextSleepDelay / 1000.0f) + " second(s)."); // We can retry again so increment a counter to keep track of the number of times we retried and recalculate the total elapsed time. retryCount++; // Sleep for the next sleep delay. herdThreadHelper.sleep(nextSleepDelay); } else { // It's not a retryable exception (i.e. some other type of service exception) so just re-throw it. throw ase; } } } }