RetryCondition setCredentials не работает - PullRequest
0 голосов
/ 29 мая 2020

Я пытаюсь установить учетные данные для этого запроса, когда хочу повторить попытку, но каким-то образом новые учетные данные не используются. Если я отлаживаю код и использую этот поставщик учетных данных в окне просмотра IntelliJ, он работает. Не уверен, в чем проблема.

В идеале, согласно моему пониманию документации, этот поставщик учетных данных должен работать для этого конкретного запроса, переопределяя поставщика учетных данных по умолчанию для клиента.

public class CustomAWSRetryCondition implements RetryPolicy.RetryCondition {
@Override
public boolean shouldRetry(AmazonWebServiceRequest originalRequest,
                           AmazonClientException exception,
                           int retriesAttempted) {
    if (exception.getCause() instanceof IOException) {
        return true;
    }

    if (exception instanceof AmazonS3Exception) {
        if (true) {
            try {
                AWSStaticCredentialsProvider awsStaticCredentialsProvider = SingletonAWSClientsProvider.reinitializeAndProvideS3Client();
                awsStaticCredentialsProvider.refresh();
                originalRequest.setRequestCredentialsProvider(awsStaticCredentialsProvider);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
        if (((AmazonS3Exception) exception).getErrorCode().equals("ExpiredToken") || ((AmazonS3Exception) exception).getErrorCode().equals("InvalidToken")) {
            try {
                Thread.sleep(10000);
                originalRequest.setRequestCredentialsProvider(SingletonAWSClientsProvider.reinitializeAndProvideS3Client());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    }

    // Only retry on a subset of service exceptions
    if (exception instanceof AmazonServiceException) {
        AmazonServiceException ase = (AmazonServiceException) exception;

        /*
         * For 500 internal server errors and 503 service
         * unavailable errors, we want to retry, but we need to use
         * an exponential back-off strategy so that we don't overload
         * a server with a flood of retries.
         */
        if (RetryUtils.isRetryableServiceException(ase)) {
            return true;
        }

        /*
         * Throttling is reported as a 400 error from newer services. To try
         * and smooth out an occasional throttling error, we'll pause and
         * retry, hoping that the pause is long enough for the request to
         * get through the next time.
         */
        if (RetryUtils.isThrottlingException(ase)) {
            return true;
        }

        /*
         * Clock skew exception. If it is then we will get the time offset
         * between the device time and the server time to set the clock skew
         * and then retry the request.
         */
        if (RetryUtils.isClockSkewError(ase)) {
            return true;
        }
    }

    return false;
}

}

...