发送状态更改电子邮件

时间:2014-08-13 10:01:05

标签: java multithreading spring email spring-mvc

我有一个项目,目前只运行网站时的方法。现在我需要实现一个线程,它检查数据库/网站的状态变化,这样我就可以发送邮件。我已成功添加了向某人邮寄的功能,并且还知道如何执行此操作的逻辑。当我尝试编写代码时会出现问题,我不知道该怎么做(因为我主要不知道后端,现在我对项目负责。

逻辑是实现一个java线程(runnable)然后用新的颜色检查前一个颜色,如果它已经改变发送邮件(颜色是状态,例如,绿色和红色)。什么是最容易理解和简单的方法?我将编写函数名称,以便了解我的内容。

我已经坚持了一个星期而且不知道该怎么做。任何帮助将不胜感激。

类中改变颜色的对象的方法:

    public void setColour(List<Status> statusar, List<Category> subcategories) {
    for (int index = 0; index < subcategories.size(); index++) {
        if (this.statusColor.compareToIgnoreCase("red") != 0) {

            if ((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("green") == 0)
                    && (this.priority < ((Category)subcategories.get(index)).getPriority())) {
                this.statusColor = "GREEN";
                this.priority = ((Category)subcategories.get(index)).getPriority();
            }

            if (((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("red") == 0)
                    && (((Category)subcategories.get(index)).getPriority() == 2))
                    || ((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("yellow") == 0)
                    && (this.priority <= ((Category)subcategories.get(index)).getPriority()))
                    || ((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("yellow") == 0)
                    && (((Category)subcategories.get(index)).getPriority() == 3))) {
                this.statusColor = "YELLOW";
                this.priority = ((Category)subcategories.get(index)).getPriority();
            }

            if (((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("red") == 0)
                    && (((Category)subcategories.get(index)).getPriority() == 3))
                    || ((((Status)statusar.get(index)).getStatusColor().compareToIgnoreCase("red") == 0)
                    && (this.priority <= ((Category)subcategories.get(index)).getPriority()))) {
                this.statusColor = "RED";
                this.priority = ((Category)subcategories.get(index)).getPriority();
            }
        }
    }
}

我在数据库部分找到的颜色功能:

public List<Map<String, Object>> listColorsOverDays(String days, String categoryName) {
    String SQL_getColors = "SELECT COUNT(*) count,color FROM ( ";
    SQL_getColors += " SELECT CASE";
    SQL_getColors += " WHEN CAST(status.value AS DECIMAL) >= CAST(greenFrom AS DECIMAL) AND CAST(status.value AS DECIMAL) <= CAST(greenTo AS DECIMAL) THEN 'GREEN' ";
    SQL_getColors += " WHEN CAST(status.value AS DECIMAL) >= CAST(yellowFrom AS DECIMAL) AND CAST(status.value AS DECIMAL) <= CAST(yellowTo AS DECIMAL) THEN 'YELLOW' ";
    SQL_getColors += " ELSE 'RED' END AS color ";
    SQL_getColors += " FROM status INNER JOIN category ON status.idCategory = category.idCategory ";
    SQL_getColors += " INNER JOIN threshold ON category.idCategory = threshold.idCategory ";
    SQL_getColors += " WHERE status.timeStamp>DATE_SUB(NOW(), INTERVAL " + days + " DAY) ";

    SQL_getColors += " AND category.name = '"+categoryName+"'";

    SQL_getColors += " ) as p group by p.color";

    List<Map<String, Object>> colorList = null;
    try {
        colorList = getJdbcTemplate().queryForList(SQL_getColors);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return colorList;
}

这是使用此功能的控制器:

public List<Map<String, Object>> getColorList(String days, String categoryName) {
    StatusDAO statusDao_i = (StatusDAO)this.context.getBean("statusDAO");
    List<Map<String, Object>> colorList = new ArrayList();
    try {
        colorList = statusDao_i.listColorsOverDays(days, categoryName);
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return colorList;

}

包含颜色的状态列表:

public List<Status> getStatusList(Status status) {
    StatusDAO statusDao_i = (StatusDAO)this.context.getBean("statusDAO");
    status.setCategoryId(this.categoryService_i.getCategoryId(status.getCategoryName()));

    Timestamp fromTime = Timestamp.valueOf(status.getFromTime());
    Timestamp toTime = Timestamp.valueOf(status.getToTime());
    List<Status> statusList = new ArrayList();
    try {
        statusList = statusDao_i.getStatusesByTime(status, fromTime, toTime);
        status.setCategoryId(this.categoryService_i.getCategoryId(status.getCategoryName()));
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusList;
}

0 个答案:

没有答案
相关问题