Example usage for com.amazonaws.services.simpleemail.model SendEmailRequest withDestination

List of usage examples for com.amazonaws.services.simpleemail.model SendEmailRequest withDestination

Introduction

In this page you can find the example usage for com.amazonaws.services.simpleemail.model SendEmailRequest withDestination.

Prototype


public SendEmailRequest withDestination(Destination destination) 

Source Link

Document

The destination for this email, composed of To:, CC:, and BCC: fields.

Usage

From source file:com.netflix.ice.processor.BillingFileProcessor.java

License:Apache License

private void sendOndemandCostAlert() {

    if (ondemandThreshold == null || StringUtils.isEmpty(fromEmail) || StringUtils.isEmpty(alertEmails)
            || endMilli < lastAlertMillis() + AwsUtils.hourMillis * 24)
        return;/*from   w w  w . j  a v a2 s.  c  o  m*/

    Map<Long, Map<Ec2InstanceReservationPrice.Key, Double>> ondemandCosts = getOndemandCosts(
            lastAlertMillis() + AwsUtils.hourMillis);
    Long maxHour = null;
    double maxTotal = ondemandThreshold;

    for (Long hour : ondemandCosts.keySet()) {
        double total = 0;
        for (Double value : ondemandCosts.get(hour).values())
            total += value;

        if (total > maxTotal) {
            maxHour = hour;
            maxTotal = total;
        }
    }

    if (maxHour != null) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
        String subject = String.format("Alert: Ondemand cost per hour reached $%s at %s",
                numberFormat.format(maxTotal), AwsUtils.dateFormatter.print(maxHour));
        StringBuilder body = new StringBuilder();
        body.append(String.format("Total ondemand cost $%s at %s:<br><br>", numberFormat.format(maxTotal),
                AwsUtils.dateFormatter.print(maxHour)));
        TreeMap<Double, String> costs = Maps.newTreeMap();
        for (Map.Entry<Ec2InstanceReservationPrice.Key, Double> entry : ondemandCosts.get(maxHour).entrySet()) {
            costs.put(entry.getValue(), entry.getKey().region + " " + entry.getKey().usageType + ": ");
        }
        for (Double cost : costs.descendingKeySet()) {
            if (cost > 0)
                body.append(costs.get(cost)).append("$" + numberFormat.format(cost)).append("<br>");
        }
        body.append("<br>Please go to <a href=\"" + urlPrefix
                + "dashboard/reservation#usage_cost=cost&groupBy=UsageType&product=ec2_instance&operation=OndemandInstances\">Ice</a> for details.");
        SendEmailRequest request = new SendEmailRequest();
        request.withSource(fromEmail);
        List<String> emails = Lists.newArrayList(alertEmails.split(","));
        request.withDestination(new Destination(emails));
        request.withMessage(
                new Message(new Content(subject), new Body().withHtml(new Content(body.toString()))));

        AmazonSimpleEmailServiceClient emailService = AwsUtils.getAmazonSimpleEmailServiceClient();
        try {
            emailService.sendEmail(request);
            updateLastAlertMillis(endMilli);
            logger.info("updateLastAlertMillis " + endMilli);
        } catch (Exception e) {
            logger.error("Error in sending alert emails", e);
        }
    }
}