List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client createSnapshot
@Override
public CreateSnapshotResult createSnapshot(CreateSnapshotRequest request)
Creates a snapshot of an EBS volume and stores it in Amazon S3.
From source file:com.pearson.eidetic.aws.EC2ClientMethods.java
public static CreateSnapshotResult createSnapshot(AmazonEC2Client ec2Client, CreateSnapshotRequest snapshotRequest, Integer numRetries, Integer maxApiRequestsPerSecond, String uniqueAwsAccountIdentifier) { CreateSnapshotResult createSnapshotResult = null; for (int i = 0; i <= numRetries; i++) { try {//from w w w . j a v a 2s. c o m // if the initial download attempt failed, wait for i * 500ms if (i > 0) { long sleepTimeInMilliseconds = 500 * i; Threads.sleepMilliseconds(sleepTimeInMilliseconds); logger.debug(snapshotRequest.toString() + System.lineSeparator() + "**********I am sleeping because something is blocking on AWS****************"); } AtomicLong requestAttemptCounter = GlobalVariables.apiRequestAttemptCountersByAwsAccount .get(uniqueAwsAccountIdentifier); long currentRequestCount = requestAttemptCounter.incrementAndGet(); while (currentRequestCount > maxApiRequestsPerSecond) { Threads.sleepMilliseconds(50); currentRequestCount = requestAttemptCounter.incrementAndGet(); logger.debug(snapshotRequest.toString() + System.lineSeparator() + "****** I am waiting because the request rate is being throttled by Eidetic"); } Long elapsedTime; Long startTime = System.currentTimeMillis(); createSnapshotResult = ec2Client.createSnapshot(snapshotRequest); elapsedTime = (new Date()).getTime() - startTime; if (createSnapshotResult != null) { GlobalVariables.apiRequestCountersByAwsAccount.get(uniqueAwsAccountIdentifier) .incrementAndGet(); logger.debug("************** I finished this snapshot and it took " + elapsedTime.toString() + " ms *******************"); break; } } catch (Exception e) { if (e.toString().contains("retired")) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\"," + snapshotRequest.toString() + System.lineSeparator() + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); return createSnapshotResult; } logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\"," + snapshotRequest.toString() + System.lineSeparator() + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } } logger.info("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\",Event=\"Snapshot Created\", Volume_id=\"" + snapshotRequest.getVolumeId() + "\""); return createSnapshotResult; }