Example usage for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryRequest withNextToken

List of usage examples for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryRequest withNextToken

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryRequest withNextToken.

Prototype


public DescribeSpotPriceHistoryRequest withNextToken(String nextToken) 

Source Link

Document

The token for the next set of results.

Usage

From source file:imperial.modaclouds.monitoring.datacollectors.monitors.EC2SpotPriceMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;//from ww  w.ja v  a2 s.co  m

    String secretKey = null;

    long startTime = 0;

    while (!spmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {
            spotInstanceVec = new ArrayList<SpotInstance>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {

                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        String endpoint = null;
                        String productDes = null;
                        String instanceType = null;

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        endpoint = parameters.get("endPoint");
                        productDes = parameters.get("productDescription");
                        instanceType = parameters.get("productDescription");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                        samplingProb = Double.valueOf(parameters.get("samplingProbability"));

                        SpotInstance spotInstance = new SpotInstance();
                        spotInstance.productDes = new ArrayList<String>();
                        spotInstance.instanceType = new ArrayList<String>();

                        spotInstance.endpoint = endpoint;
                        spotInstance.productDes.add(productDes);
                        spotInstance.instanceType.add(instanceType);

                        spotInstanceVec.add(spotInstance);
                    }
                } catch (NumberFormatException | ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();
        }

        AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);

        //////

        boolean isSent = false;
        if (Math.random() < this.samplingProb) {
            isSent = true;
        }

        for (int i = 0; i < spotInstanceVec.size(); i++) {
            ec2.setEndpoint(spotInstanceVec.get(i).endpoint);

            DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest();

            request.setProductDescriptions(spotInstanceVec.get(i).productDes);
            request.setInstanceTypes(spotInstanceVec.get(i).instanceType);
            //request.setStartTime(startTime);
            //request.setMaxResults(maxResult);

            String nextToken = "";
            //do {
            request.withNextToken(nextToken);

            DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory(request);

            List<String> splitString = Arrays.asList(result.getSpotPriceHistory().get(0).toString().split(","));

            if (isSent) {
                for (String temp : splitString) {
                    if (temp.contains("SpotPrice")) {
                        temp = temp.replace("SpotPrice: ", "");
                        System.out.println(temp);
                        try {
                            logger.info("Sending datum: {} {} {}", temp, "SpotPrice", monitoredTarget);
                            dcAgent.send(
                                    new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId()),
                                    "SpotPrice", temp);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            //for (int j = 0; j < result.getSpotPriceHistory().size(); j++) {
            //System.out.println(result.getSpotPriceHistory().get(0));
            //break;
            //}

            //result = result.withNextToken(result.getNextToken());
            //nextToken = result.getNextToken();
            //} while(!nextToken.isEmpty());
        }
        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}