Retries
Example in Java
The following example is not meant to be complete, its goal is to give you a good idea of how to configure your AWS clients.
import com.amazonaws.ClientConfiguration;
import com.amazonaws.retry.PredefinedBackoffStrategies.SDKDefaultBackoffStrategy;
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
public class Main {
private static ClientConfiguration getClientConfiguration() {
// Retry Wait time (ms)
// 0 0
// 1 1,000
// 2 2,000
// 3 4,000
// 4 8,000
// 5 16,000
final int baseDelay = 500;
final int throttledBaseDelay = 1_000;
final int maxBackoff = 16_000;
final int maxErrorRetry = 5;
SDKDefaultBackoffStrategy backoffStrategy = new SDKDefaultBackoffStrategy(baseDelay, throttledBaseDelay, maxBackoff);
final RetryPolicy retryPolicy = new RetryPolicy(
PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
backoffStrategy,
maxErrorRetry,
true
);
// Passing the default values (just being explicit)
return new ClientConfiguration()
.withSocketTimeout(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT)
.withConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT)
.withMaxConnections(ClientConfiguration.DEFAULT_MAX_CONNECTIONS)
.withRetryPolicy(retryPolicy);
}
public static void main(String[] args) {
AmazonSQS client = AmazonSQSClientBuilder.standard()
.withClientConfiguration(getClientConfiguration())
.withRegion("us-east-1")
.build();
// Use the client
}
}
import com.amazonaws.ClientConfiguration;
import com.amazonaws.retry.PredefinedBackoffStrategies.SDKDefaultBackoffStrategy;
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
public class Main {
private static ClientConfiguration getClientConfiguration() {
// Retry Wait time (ms)
// 0 0
// 1 1,000
// 2 2,000
// 3 4,000
// 4 8,000
// 5 16,000
final int baseDelay = 500;
final int throttledBaseDelay = 1_000;
final int maxBackoff = 16_000;
final int maxErrorRetry = 5;
SDKDefaultBackoffStrategy backoffStrategy = new SDKDefaultBackoffStrategy(baseDelay, throttledBaseDelay, maxBackoff);
final RetryPolicy retryPolicy = new RetryPolicy(
PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
backoffStrategy,
maxErrorRetry,
true
);
// Passing the default values (just being explicit)
return new ClientConfiguration()
.withSocketTimeout(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT)
.withConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT)
.withMaxConnections(ClientConfiguration.DEFAULT_MAX_CONNECTIONS)
.withRetryPolicy(retryPolicy);
}
public static void main(String[] args) {
AmazonSQS client = AmazonSQSClientBuilder.standard()
.withClientConfiguration(getClientConfiguration())
.withRegion("us-east-1")
.build();
// Use the client
}
}
import com.amazonaws.ClientConfiguration; import com.amazonaws.retry.PredefinedBackoffStrategies.SDKDefaultBackoffStrategy; import com.amazonaws.retry.PredefinedRetryPolicies; import com.amazonaws.retry.RetryPolicy; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; public class Main { private static ClientConfiguration getClientConfiguration() { // Retry Wait time (ms) // 0 0 // 1 1,000 // 2 2,000 // 3 4,000 // 4 8,000 // 5 16,000 final int baseDelay = 500; final int throttledBaseDelay = 1_000; final int maxBackoff = 16_000; final int maxErrorRetry = 5; SDKDefaultBackoffStrategy backoffStrategy = new SDKDefaultBackoffStrategy(baseDelay, throttledBaseDelay, maxBackoff); final RetryPolicy retryPolicy = new RetryPolicy( PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, backoffStrategy, maxErrorRetry, true ); // Passing the default values (just being explicit) return new ClientConfiguration() .withSocketTimeout(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT) .withConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT) .withMaxConnections(ClientConfiguration.DEFAULT_MAX_CONNECTIONS) .withRetryPolicy(retryPolicy); } public static void main(String[] args) { AmazonSQS client = AmazonSQSClientBuilder.standard() .withClientConfiguration(getClientConfiguration()) .withRegion("us-east-1") .build(); // Use the client } }