本文整理匯總了Java中com.amazonaws.services.ec2.AmazonEC2.requestSpotInstances方法的典型用法代碼示例。如果您正苦於以下問題:Java AmazonEC2.requestSpotInstances方法的具體用法?Java AmazonEC2.requestSpotInstances怎麽用?Java AmazonEC2.requestSpotInstances使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.amazonaws.services.ec2.AmazonEC2
的用法示例。
在下文中一共展示了AmazonEC2.requestSpotInstances方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createInstances
import com.amazonaws.services.ec2.AmazonEC2; //導入方法依賴的package包/類
public void createInstances(int count, double priceLimitDollars) {
AmazonEC2 ec2 = new AmazonEC2Client(getAwsCredentials());
ec2.setEndpoint(getOrCry("ec2endpoint"));
log.info("Requesting " + count + " instances of type "
+ getOrCry("ec2instancetype") + " with price limit of "
+ priceLimitDollars + " US$");
log.debug(startupScript);
try {
// our bid
RequestSpotInstancesRequest runInstancesRequest = new RequestSpotInstancesRequest()
.withSpotPrice(Double.toString(priceLimitDollars))
.withInstanceCount(count).withType("persistent");
// increase volume size
// BlockDeviceMapping mapping = new BlockDeviceMapping()
// .withDeviceName("/dev/sda1").withEbs(
// new EbsBlockDevice().withVolumeSize(Integer
// .parseInt(getOrCry("ec2disksize"))));
// what we want
LaunchSpecification workerSpec = new LaunchSpecification()
.withInstanceType(getOrCry("ec2instancetype"))
.withImageId(getOrCry("ec2ami"))
.withKeyName(getOrCry("ec2keypair"))
// .withBlockDeviceMappings(mapping)
.withUserData(
new String(Base64.encodeBase64(startupScript
.getBytes())));
runInstancesRequest.setLaunchSpecification(workerSpec);
// place the request
ec2.requestSpotInstances(runInstancesRequest);
log.info("Request placed, now use 'monitor' to check how many instances are running. Use 'shutdown' to cancel the request and terminate the corresponding instances.");
} catch (Exception e) {
log.warn("Failed to start instances - ", e);
}
}
示例2: main
import com.amazonaws.services.ec2.AmazonEC2; //導入方法依賴的package包/類
public static void main(String[] args) {
String region = "us-east-1";
AWSCredentials credentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey);
AmazonEC2 api = AmazonEC2ClientBuilder.standard().withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
// no particular availability zone
String availabilityZone = null;
String instanceType = "t1.micro";
String imageId = "ami-3cf8b154";
List<String> securityGroups = Arrays.asList("webserver");
String keyPair = "instancekey";
String bootScript = Joiner.on("\n")
.join(Arrays.asList("#!/bin/bash", "sudo apt-get update -qy", "sudo apt-get install -qy apache2"));
int instanceCount = 50;
String bidPrice = "0.001";
SpotPlacement placement = new SpotPlacement().withAvailabilityZone(availabilityZone);
LaunchSpecification launchSpec = new LaunchSpecification().withInstanceType(instanceType).withImageId(imageId)
.withPlacement(placement).withSecurityGroups(securityGroups).withKeyName(keyPair)
.withUserData(AmazonApiUtils.base64Encode(bootScript));
RequestSpotInstancesRequest request = new RequestSpotInstancesRequest().withInstanceCount(instanceCount)
.withType(SpotInstanceType.Persistent).withSpotPrice(bidPrice).withLaunchSpecification(launchSpec);
RequestSpotInstancesResult result = api.requestSpotInstances(request);
for (SpotInstanceRequest spotRequest : result.getSpotInstanceRequests()) {
LOG.info("placed request: {}", spotRequest.getSpotInstanceRequestId());
}
}
示例3: execute
import com.amazonaws.services.ec2.AmazonEC2; //導入方法依賴的package包/類
@Override
public void execute(AmazonEC2 client, Pool pool, DelegateExecution execution) throws IOException {
/* before sending a new request, we check to see if we already registered
a launch group with the process ID, if yes, we don't re-send the request */
final String businessKey = execution.getProcessBusinessKey();
/* we timeout if requests have already been sent - the activity is being retried. */
Optional<Object> alreadySent = Optional.fromNullable(
execution.getVariable(ProcessVariables.SPOT_INSTANCE_REQUEST_IDS));
if (alreadySent.isPresent()) {
DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest()
.withFilters(new Filter()
.withName("launch-group").withValues(businessKey)
.withName("state").withValues("open", "active"));
Stopwatch stopwatch = new Stopwatch().start();
while (stopwatch.elapsedTime(TimeUnit.MINUTES) < 2) {
DescribeSpotInstanceRequestsResult result = client.describeSpotInstanceRequests(describeRequest);
List<SpotInstanceRequest> pending = result.getSpotInstanceRequests();
if (pending.size() > 0) {
LOG.info("Not resending spot instance requests {} for businessKey: {}.", pending, businessKey);
execution.setVariable(ProcessVariables.SPOT_INSTANCE_REQUEST_IDS,
collectSpotInstanceRequestIds(pending));
return;
}
LOG.info("The describe call has not returned anything yet, waiting 20s and retrying.");
Uninterruptibles.sleepUninterruptibly(20, TimeUnit.SECONDS);
}
}
final RequestSpotInstancesRequest request = createSpotInstancesRequest(pool, execution);
execution.setVariable(ProcessVariables.SPOT_REQUESTS_SENT, true);
RequestSpotInstancesResult requestResult = client.requestSpotInstances(request);
List<String> spotInstanceRequestIds = collectSpotInstanceRequestIds(requestResult.getSpotInstanceRequests());
execution.setVariable(ProcessVariables.SPOT_INSTANCE_REQUEST_IDS, spotInstanceRequestIds);
}