User guide
// Add all of the request ids to the hashset, so we can
120 // determine when they hit the active state.
for (SpotInstanceRequest requestResponse : requestResponses) {
System.out.println("Created Spot Request: "+requestResponse.getSpot
InstanceRequestId());
spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
}
125
//==========================================================//
//============= Tag the Spot Requests ======================//
//==========================================================//
130 // Create the list of tags we want to create
ArrayList<Tag> requestTags = new ArrayList<Tag>();
requestTags.add(new Tag("keyname1","value1"));
// Create a tag request for the requests.
135 CreateTagsRequest createTagsRequest_requests = new CreateTagsRequest();
createTagsRequest_requests.setResources(spotInstanceRequestIds);
createTagsRequest_requests.setTags(requestTags);
// Try to tag the Spot request submitted.
140 try {
ec2.createTags(createTagsRequest_requests);
} catch (AmazonServiceException e) {
// Write out any exceptions that may have occurred.
System.out.println("Error terminating instances");
145 System.out.println("Caught Exception: " + e.getMessage());
System.out.println("Reponse Status Code: " + e.getStatusCode());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Request ID: " + e.getRequestId());
}
150
//===========================================================//
//======= Determining the State of the Spot Request =========//
//===========================================================//
155 // Create a variable that will track whether there are any
// requests still in the open state.
boolean anyOpen;
// Initialize variables.
160 ArrayList<String> instanceIds = new ArrayList<String>();
do {
// Create the describeRequest with tall of the request
// id to monitor (e.g. that we started).
165 DescribeSpotInstanceRequestsRequest describeRequest = new DescribeS
potInstanceRequestsRequest();
describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);
// Initialize the anyOpen variable to false - which assumes there are
no requests open unless
// we find one that is still open.
170 anyOpen = false;
try {
// Retrieve all of the requests we want to monitor.
Version v1.0.0
60
AWS SDK for Java Developer Guide
Tutorial: Advanced Amazon EC2 Spot Request
Management