Example usage for com.amazonaws ClientConfiguration setRetryPolicy

List of usage examples for com.amazonaws ClientConfiguration setRetryPolicy

Introduction

In this page you can find the example usage for com.amazonaws ClientConfiguration setRetryPolicy.

Prototype

public void setRetryPolicy(RetryPolicy retryPolicy) 

Source Link

Document

Sets the retry policy upon failed requests.

Usage

From source file:com.gu.logback.appender.kinesis.BaseKinesisAppender.java

License:Open Source License

/**
 * Configures appender instance and makes it ready for use by the consumers.
 * It validates mandatory parameters and confirms if the configured stream is
 * ready for publishing data yet.//from w  w  w  .j a v a  2s.c  o m
 * 
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException if we encounter issues configuring this
 *           appender instance
 */
@Override
public void start() {
    if (layout == null) {
        initializationFailed = true;
        addError("Invalid configuration - No layout for appender: " + name);
        return;
    }

    if (streamName == null) {
        initializationFailed = true;
        addError("Invalid configuration - streamName cannot be null for appender: " + name);
        return;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();

    this.client = createClient(credentials, clientConfiguration, threadPoolExecutor);

    client.setRegion(findRegion());
    if (!Validator.isBlank(endpoint)) {
        if (!Validator.isBlank(region)) {
            addError("Received configuration for both region as well as Amazon Kinesis endpoint. (" + endpoint
                    + ") will be used as endpoint instead of default endpoint for region (" + region + ")");
        }
        client.setEndpoint(endpoint);
    }

    validateStreamName(client, streamName);

    super.start();
}

From source file:com.gu.logback.appender.kinesis.KinesisAppender.java

License:Open Source License

/**
 * Configures this appender instance and makes it ready for use by the
 * consumers. It validates mandatory parameters and confirms if the configured
 * stream is ready for publishing data yet.
 * // w  w  w  . j  a v  a2s  . c o m
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException
 *           if we encounter issues configuring this appender instance
 */
@Override
public void start() {
    if (layout == null) {
        initializationFailed = true;
        addError("Invalid configuration - No layout for appender: " + name);
        return;
    }

    if (streamName == null) {
        initializationFailed = true;
        addError("Invalid configuration - streamName cannot be null for appender: " + name);
        return;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();
    kinesisClient = new AmazonKinesisAsyncClient(credentials, clientConfiguration, threadPoolExecutor);

    boolean regionProvided = !Validator.isBlank(region);
    if (!regionProvided) {
        region = AppenderConstants.DEFAULT_REGION;
    }
    kinesisClient.setRegion(Region.getRegion(Regions.fromName(region)));
    if (!Validator.isBlank(endpoint)) {
        if (regionProvided) {
            addError("Received configuration for both region as well as Amazon Kinesis endpoint. (" + endpoint
                    + ") will be used as endpoint instead of default endpoint for region (" + region + ")");
        }
        kinesisClient.setEndpoint(endpoint);
    }

    DescribeStreamResult describeResult = null;
    try {
        describeResult = kinesisClient.describeStream(streamName);
        String streamStatus = describeResult.getStreamDescription().getStreamStatus();
        if (!StreamStatus.ACTIVE.name().equals(streamStatus)
                && !StreamStatus.UPDATING.name().equals(streamStatus)) {
            initializationFailed = true;
            addError(
                    "Stream " + streamName + " is not ready (in active/updating status) for appender: " + name);
        }
    } catch (ResourceNotFoundException rnfe) {
        initializationFailed = true;
        addError("Stream " + streamName + " doesn't exist for appender: " + name, rnfe);
    }

    asyncCallHander = new AsyncPutCallStatsReporter(this);

    super.start();
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.AmazonClientProvider.java

License:Apache License

protected <T extends AmazonWebServiceClient> T getClient(Class<T> impl,
        AWSCredentialsProvider awsCredentialsProvider, String region) {
    checkAWSCredentialsProvider(awsCredentialsProvider);
    try {//ww  w.  j  a va 2 s .  c  o  m
        Constructor<T> constructor = impl.getConstructor(AWSCredentialsProvider.class,
                ClientConfiguration.class);

        ClientConfiguration clientConfiguration = new ClientConfiguration();

        if (awsCredentialsProvider instanceof NetflixSTSAssumeRoleSessionCredentialsProvider) {
            RetryPolicy.RetryCondition delegatingRetryCondition = (originalRequest, exception,
                    retriesAttempted) -> {
                NetflixSTSAssumeRoleSessionCredentialsProvider stsCredentialsProvider = (NetflixSTSAssumeRoleSessionCredentialsProvider) awsCredentialsProvider;
                if (exception instanceof AmazonServiceException) {
                    ((AmazonServiceException) exception).getHttpHeaders().put("targetAccountId",
                            stsCredentialsProvider.getAccountId());
                }
                return retryPolicy.getRetryCondition().shouldRetry(originalRequest, exception,
                        retriesAttempted);
            };

            RetryPolicy delegatingRetryPolicy = new RetryPolicy(delegatingRetryCondition,
                    retryPolicy.getBackoffStrategy(), retryPolicy.getMaxErrorRetry(),
                    retryPolicy.isMaxErrorRetryInClientConfigHonored());
            clientConfiguration.setRetryPolicy(delegatingRetryPolicy);
        } else {
            clientConfiguration.setRetryPolicy(retryPolicy);
        }

        if (proxy != null && proxy.isProxyConfigMode()) {
            proxy.apply(clientConfiguration);
        }

        clientConfiguration.setUseGzip(useGzip);

        T delegate = constructor.newInstance(awsCredentialsProvider, clientConfiguration);
        for (RequestHandler2 requestHandler : requestHandlers) {
            delegate.addRequestHandler(requestHandler);
        }
        if (region != null && region.length() > 0) {
            delegate.setRegion(Region.getRegion(Regions.fromName(region)));
        }
        return delegate;
    } catch (Exception e) {
        throw new RuntimeException("Instantiation of client implementation failed!", e);
    }
}

From source file:com.tcl.gateway.firehose.log4j.FirehoseAppender.java

License:Open Source License

/**
 * Configures this appender instance and makes it ready for use by the
 * consumers. It validates mandatory parameters and confirms if the configured
 * stream is ready for publishing data yet.
 * /*from  ww w . ja  va 2s .  c  om*/
 * Error details are made available through the fallback handler for this
 * appender
 * 
 * @throws IllegalStateException
 *           if we encounter issues configuring this appender instance
 */
@Override
public void activateOptions() {
    if (deliveryStreamName == null) {
        initializationFailed = true;
        error("Invalid configuration - streamName cannot be null for appender: " + name);
    }

    if (layout == null) {
        initializationFailed = true;
        error("Invalid configuration - No layout for appender: " + name);
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration = setProxySettingsFromSystemProperties(clientConfiguration);

    clientConfiguration.setMaxErrorRetry(maxRetries);
    clientConfiguration.setRetryPolicy(new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
            PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, maxRetries, true));
    clientConfiguration.setUserAgent(AppenderConstants.USER_AGENT_STRING);

    final BlockingQueue<Runnable> taskBuffer = new LinkedBlockingDeque<Runnable>(bufferSize);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount,
            AppenderConstants.DEFAULT_THREAD_KEEP_ALIVE_SEC, TimeUnit.SECONDS, taskBuffer,
            new BlockFastProducerPolicy());
    threadPoolExecutor.prestartAllCoreThreads();
    firehoseClient = new AmazonKinesisFirehoseAsyncClient(new CustomCredentialsProviderChain(),
            clientConfiguration, threadPoolExecutor);

    boolean regionProvided = !Validator.isBlank(region);
    if (!regionProvided) {
        region = AppenderConstants.DEFAULT_REGION;
    }
    if (!Validator.isBlank(endpoint)) {
        if (regionProvided) {
            LOGGER.warn("Received configuration for both region as well as Amazon Kinesis endpoint. ("
                    + endpoint + ") will be used as endpoint instead of default endpoint for region (" + region
                    + ")");
        }
        firehoseClient.setEndpoint(endpoint);
    } else {
        firehoseClient.setRegion(Region.getRegion(Regions.fromName(region)));
    }

    DescribeDeliveryStreamResult describeResult = null;
    try {
        describeResult = firehoseClient.describeDeliveryStream(
                new DescribeDeliveryStreamRequest().withDeliveryStreamName(deliveryStreamName));
        String streamStatus = describeResult.getDeliveryStreamDescription().getDeliveryStreamStatus();
        if (!StreamStatus.ACTIVE.name().equals(streamStatus)
                && !StreamStatus.UPDATING.name().equals(streamStatus)) {
            initializationFailed = true;
            error("Delivery Stream " + deliveryStreamName
                    + " is not ready (in active/updating status) for appender: " + name);
        }
    } catch (ResourceNotFoundException rnfe) {
        initializationFailed = true;
        error("Delivery  Stream " + deliveryStreamName + " doesn't exist for appender: " + name, rnfe);
    }

    asyncCallHander = new AsyncPutCallStatsReporter(name);

    if (metric) {
        MetricRegistry registry = new MetricRegistry();
        registry.register("Gauge", new Gauge<Integer>() {

            @Override
            public Integer getValue() {
                return taskBuffer.size();
            }

        });

        ConsoleReporter.forRegistry(registry).build().start(3, TimeUnit.SECONDS);
    }
}

From source file:com.zanox.vertx.mods.KinesisMessageProcessor.java

License:Apache License

private AmazonKinesisAsyncClient createClient() {

    // Building Kinesis configuration
    int connectionTimeout = getOptionalIntConfig(CONNECTION_TIMEOUT,
            ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);
    int maxConnection = getOptionalIntConfig(MAX_CONNECTION, ClientConfiguration.DEFAULT_MAX_CONNECTIONS);

    // TODO: replace default retry policy
    RetryPolicy retryPolicy = ClientConfiguration.DEFAULT_RETRY_POLICY;
    int socketTimeout = getOptionalIntConfig(SOCKET_TIMEOUT, ClientConfiguration.DEFAULT_SOCKET_TIMEOUT);
    boolean useReaper = getOptionalBooleanConfig(USE_REAPER, ClientConfiguration.DEFAULT_USE_REAPER);
    String userAgent = getOptionalStringConfig(USER_AGENT, ClientConfiguration.DEFAULT_USER_AGENT);

    streamName = getMandatoryStringConfig(STREAM_NAME);
    partitionKey = getMandatoryStringConfig(PARTITION_KEY);
    region = getMandatoryStringConfig(REGION);

    logger.info(" --- Stream name: " + streamName);
    logger.info(" --- Partition key: " + partitionKey);
    logger.info(" --- Region: " + region);

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setConnectionTimeout(connectionTimeout);
    clientConfiguration.setMaxConnections(maxConnection);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout(socketTimeout);
    clientConfiguration.setUseReaper(useReaper);
    clientConfiguration.setUserAgent(userAgent);

    /*/*from w w  w.  j a  va2  s. co  m*/
    AWS credentials provider chain that looks for credentials in this order:
       Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY
       Java System Properties - aws.accessKeyId and aws.secretKey
       Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI
       Instance profile credentials delivered through the Amazon EC2 metadata service
    */

    AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();

    // Configuring Kinesis-client with configuration
    AmazonKinesisAsyncClient kinesisAsyncClient = new AmazonKinesisAsyncClient(awsCredentialsProvider,
            clientConfiguration);
    Region awsRegion = RegionUtils.getRegion(region);
    kinesisAsyncClient.setRegion(awsRegion);

    return kinesisAsyncClient;
}

From source file:org.elasticsearch.cloud.aws.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(CLOUD_EC2.PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings) || CLOUD_EC2.PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = CLOUD_EC2.PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = CLOUD_EC2.PROXY_PORT_SETTING.get(settings);
        String proxyUsername = CLOUD_EC2.PROXY_USERNAME_SETTING.get(settings);
        String proxyPassword = CLOUD_EC2.PROXY_PASSWORD_SETTING.get(settings);

        clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort).withProxyUsername(proxyUsername)
                .withProxyPassword(proxyPassword);
    }// w w  w.jav  a  2 s .co m

    // #155: we might have 3rd party users using older EC2 API version
    String awsSigner = CLOUD_EC2.SIGNER_SETTING.get(settings);
    if (Strings.hasText(awsSigner)) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        AwsSigner.configureSigner(awsSigner, clientConfiguration);
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);

    return clientConfiguration;
}

From source file:org.elasticsearch.discovery.ec2.AwsEc2ServiceImpl.java

License:Apache License

protected static ClientConfiguration buildConfiguration(Logger logger, Settings settings) {
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    clientConfiguration.setProtocol(PROTOCOL_SETTING.get(settings));

    if (PROXY_HOST_SETTING.exists(settings)) {
        String proxyHost = PROXY_HOST_SETTING.get(settings);
        Integer proxyPort = PROXY_PORT_SETTING.get(settings);
        try (SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings);
                SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings)) {

            clientConfiguration.withProxyHost(proxyHost).withProxyPort(proxyPort)
                    .withProxyUsername(proxyUsername.toString()).withProxyPassword(proxyPassword.toString());
        }/*from   ww  w . ja  v  a 2s  .  co m*/
    }

    // Increase the number of retries in case of 5xx API responses
    final Random rand = Randomness.get();
    RetryPolicy retryPolicy = new RetryPolicy(RetryPolicy.RetryCondition.NO_RETRY_CONDITION,
            new RetryPolicy.BackoffStrategy() {
                @Override
                public long delayBeforeNextRetry(AmazonWebServiceRequest originalRequest,
                        AmazonClientException exception, int retriesAttempted) {
                    // with 10 retries the max delay time is 320s/320000ms (10 * 2^5 * 1 * 1000)
                    logger.warn("EC2 API request failed, retry again. Reason was:", exception);
                    return 1000L
                            * (long) (10d * Math.pow(2, retriesAttempted / 2.0d) * (1.0d + rand.nextDouble()));
                }
            }, 10, false);
    clientConfiguration.setRetryPolicy(retryPolicy);
    clientConfiguration.setSocketTimeout((int) READ_TIMEOUT_SETTING.get(settings).millis());

    return clientConfiguration;
}