采集系统半夜能会宕机报错等异常情况,所以有了以下简陋代码.一共两个指标.使用QUARTZ 定时器控制,采集入库的某个表N长时间没有增加数据和系统日志大小不再增加.

触发后会先去代码持有邮箱中查询是否存在带有特有符号或标识的邮件内容,如果存在就根据不同的内容,去调节!

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hna.hka.rs.tailormadereport.util.DBUtil;
import com.hna.hka.rs.tailormadereport.util.GetQQMail;
import com.hna.hka.rs.tailormadereport.util.MyEmailUtil;

@Component
public class monitorDSCS {

    private Log log = LogFactory.getLog(getClass());
    private int rowCount = 0;
    private long logSize = 0;
    private int rowError = 0;
    private int logError = 0;
    private int n = 0;

    @Scheduled(cron = "0 0 02 * * ?")
    public void initCount() {
        rowCount = 0;
        logSize = 0;
        rowError = 0;
        logError = 0;
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void isWorking() {
        int rowCountNew = 0;
        long logSizeNew = 0;
        SimpleDateFormat newSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date newDate = new Date(new Date().getTime());
        String today = newSdf.format(newDate);
        // 获取数据库驱动
        DBUtil.makeDriver();
        // 获取数据库的连接
        Connection conn = null;
        try {
            conn = DBUtil.makeConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement("select count(*) from tableName tm where tm.collection_date = to_date('" + today + "','yyyy-MM-dd')");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            rs = pstmt.executeQuery();
            while (rs.next()) {
                rowCountNew = rs.getInt(1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        File log = new File("d:" + File.separator + "logs" + File.separator + "dscs" + File.separator + "dscs.log");
        logSizeNew = log.length();
        // 第一次进来的时候只做赋值的动作
        if (rowCount == 0 && logSize == 0) {
            rowCount = rowCountNew;
            logSize = logSizeNew;
        } else {// 除去第一次之外的,都走else,
            if (rowCount >= rowCountNew) {// 判断数据库是否有新增记录
                rowError++;// 如果没有新增的记录就rowError++
            }
            if (logSize >= logSizeNew) {// 判断日志是否有更新
                logError++;// 如果日志没有更新的话那就logError++
            }
        }
        if (rowError >= n) {// 当连续两次没有更新记录的话就发送邮件
            //负数今天不再发 ,0恢复,N延迟N个小时,Integer.MAX_VALUE一直发
            int flag = GetQQMail.fetchMail();
            if (flag==Integer.MAX_VALUE) {
                List<String> logLast100Line = readLastNLine(log, 100);
                StringBuffer content = new StringBuffer("采集系统发生异常:指令" + rowError + "轮没有更新,日志文件已经" + logError + "轮次没有反生改变!最后100行日志记录如下:");
                for (int i = logLast100Line.size() - 1; i >= 0; i--) {
                    content.append(logLast100Line.get(i));
                }
                // send("******@gmail.com", "测试发送邮件",
                // "从QQ发送邮件到gmail,gmail自动转发到QQ",
                // "smtp", "smtp.qq.com", "******@qq.com此处要和后面一致", "25",
                // "*****@qq.com此处要和前面一致", "此处是邮箱加密后的密码");
                MyEmailUtil.send("4347352482149@qq.com", "预警提示", content.toString(), "smtp", "smtp.qq.com", "241542362@qq.com", "25", "241542362@qq.com", "wasdeasnfrnfdsvvcsafasdasrlyvbdsaieg");
                System.out.println("已经发送邮件!");

            } else if(flag==0){
                rowCount = 0;
                logSize = 0;
                rowError = 0;
                logError = 0;
                System.out.println("重置操作");
            }else if(flag>0&&flag<Integer.MAX_VALUE){
                rowCount = n-flag;
                logSize = n-flag;
                rowError = n-flag;
                logError = n-flag;
                System.out.println("延迟"+flag+"分钟发送");
            }else if(flag<0){
                rowCount = Integer.MIN_VALUE;
                logSize = Integer.MIN_VALUE;
                rowError = Integer.MIN_VALUE;
                logError = Integer.MIN_VALUE;
                System.out.println("今天不再提醒");
            }
        }
        try {

        } finally {
            try {
                rs.close();
                pstmt.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取文件最后N行
     * 
     * 根据换行符判断当前的行数, 使用统计来判断当前读取第N行
     * 
     * PS:输出的List是倒叙,需要对List反转输出
     * 
     * @param file
     *            待文件
     * @param numRead
     *            读取的行数
     * @return List<String>
     */
    public static List<String> readLastNLine(File file, long numRead) {
        // 定义结果集
        List<String> result = new ArrayList<String>();
        // 行数统计
        long count = 0;

        // 排除不可读状态
        if (!file.exists() || file.isDirectory() || !file.canRead()) {
            return null;
        }

        // 使用随机读取
        RandomAccessFile fileRead = null;
        try {
            // 使用读模式
            fileRead = new RandomAccessFile(file, "r");
            // 读取文件长度
            long length = fileRead.length();
            // 如果是0,代表是空文件,直接返回空结果
            if (length == 0L) {
                return result;
            } else {
                // 初始化游标
                long pos = length - 1;
                while (pos > 0) {
                    pos--;
                    // 开始读取
                    fileRead.seek(pos);
                    // 如果读取到\n代表是读取到一行
                    if (fileRead.readByte() == '\n') {
                        // 使用readLine获取当前行
                        String line = fileRead.readLine();
                        // 保存结果
                        result.add(line);

                        // 打印当前行
                        //System.out.println(line);

                        // 行数统计,如果到达了numRead指定的行数,就跳出循环
                        count++;
                        if (count == numRead) {
                            break;
                        }
                    }
                }
                if (pos == 0) {
                    fileRead.seek(0);
                    result.add(fileRead.readLine());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileRead != null) {
                try {
                    // 关闭资源
                    fileRead.close();
                } catch (Exception e) {
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
    }
}
View Code

相关文章: