邮件预警,邮件控制

采集系统半夜能会宕机报错等异常情况,所以有了以下简陋代码.一共两个指标.使用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);
                    // 如果读取到
代表是读取到一行
                    if (fileRead.readByte() == '
') {
                        // 使用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
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.search.FlagTerm;

import com.sun.mail.imap.IMAPMessage;

public class GetQQMail {

    private MimeMessage mimeMessage = null;
    private StringBuffer bodyText = new StringBuffer(); // 存放邮件内容的StringBuffer对象

    /**
     * 构造函数,初始化一个MimeMessage对象
     */
    public GetQQMail(MimeMessage mimeMessage) {
        this.mimeMessage = mimeMessage;
        // System.out.println("创建一个ReceiveEmail对象....");
    }

    /**
     * * 获得邮件正文内容
     */
    public String getBodyText() {
        return bodyText.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Boolean fetchMail = fetchMail();
        String decode = decode("PGRpdj5zZGFkc2FkYXM8L2Rpdj4=");
        System.out.println(new String(decode));
    }

    public static int fetchMail() {
        Message[] message = null;
        Folder folder = null;
        Store store = null;
        try {
            String host = "pop.qq.com";
            String username = "23424621@qq.com";
            String password = "dwefnrndvvcrlyvhbieg";//邮箱授权码
            Properties p = new Properties();
            p.setProperty("mail.pop3.host", "pop.qq.com"); // 按需要更改
            p.setProperty("mail.pop3.port", "995");
            // SSL安全连接参数
            p.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.setProperty("mail.pop3.socketFactory.fallback", "true");
            p.setProperty("mail.pop3.socketFactory.port", "995");

            Session session = Session.getInstance(p, null);
            store = session.getStore("pop3");
            store.connect(host, username, password);
            folder = store.getFolder("INBOX");
            // 如果需要在取得邮件数后将邮件置为已读则这里需要使用READ_WRITE,否则READ_ONLY就可以
            folder.open(Folder.READ_WRITE);
            message = folder.getMessages();
            // System.out.println("邮件数量: " + message.length);

            if (null != message) {
                GetQQMail re = null;
                for (int i = 0; i < message.length; i++) {
                    re = new GetQQMail((MimeMessage) message[i]);
                    re.getMailContent((Part) message[i]);
                    String bodyText = "";
                    bodyText = re.getBodyText();
                    // 负数今天不再发 ,0恢复,N延迟N个小时,Integer.MAX_VALUE一直发
                    if (bodyText.contains("忽略")) {//邮件发送忽略两个字,今天不再发送邮件提醒
                        System.out.println("包含忽略");
                        String subject = message[i].getSubject();
                        // set the DELETE flag to true
                        message[i].setFlag(Flags.Flag.DELETED, true);
                        System.out.println("Marked DELETE for message: " + subject);
                        return -1;
                    } else if (bodyText.contains("恢复")) {//恢复到正常状态
                        System.out.println("包含恢复");
                        String subject = message[i].getSubject();
                        // set the DELETE flag to true
                        message[i].setFlag(Flags.Flag.DELETED, true);
                        System.out.println("Marked DELETE for message: " + subject);
                        return 0;
                    } else if (bodyText.contains("$$$")&&bodyText.contains("&&&")) {//$$$数字&&&
                        System.out.println("包含延迟");
                        String subject = message[i].getSubject();
                        // set the DELETE flag to true
                        message[i].setFlag(Flags.Flag.DELETED, true);
                        System.out.println("Marked DELETE for message: " + subject);
                        int indexOf = bodyText.indexOf("$$$");
                        int indexOf1 = bodyText.indexOf("&&&");
                        int count = Integer.parseInt(bodyText.substring(indexOf+3, indexOf1));
                        return count;//解析出的数字就是延迟几分钟
                    }
                }
            }
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                folder.close(true);
                store.close();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return Integer.MAX_VALUE;
    }

    
    

    /**
     * * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件 *
     * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
     */

    public void getMailContent(Part part) throws Exception {

        String contentType = part.getContentType();
        // 获得邮件的MimeType类型
        System.out.println("邮件的MimeType类型: " + contentType);

        int nameIndex = contentType.indexOf("name");

        boolean conName = false;

        if (nameIndex != -1) {
            conName = true;
        }

        System.out.println("邮件内容的类型: " + contentType);

        if (part.isMimeType("text/plain") && conName == false) {
            // text/plain 类型      
            //decode 是BASE64解码里面只有内容部分是BASE64加密的,解码之后邮件内容会恢复
            String decodeStr = GetQQMail.decode((String) part.getContent());
            bodyText.append(decodeStr);
        } else if (part.isMimeType("text/html") && conName == false) {
            // text/html 类型
            //bodyText.append((String) part.getContent());
            String decodeStr = GetQQMail.decode((String) part.getContent());
            bodyText.append(decodeStr);
        } else if (part.isMimeType("multipart/*")) {
            // multipart/*
            Multipart multipart = (Multipart) part.getContent();
            int counts = multipart.getCount();
            for (int i = 0; i < counts; i++) {
                getMailContent(multipart.getBodyPart(i));
            }
        } else if (part.isMimeType("message/rfc822")) {
            // message/rfc822
            getMailContent((Part) part.getContent());
        } else {

        }
    }
    
     public static String decode(String str){    
           byte[] bt = null;    
           try {    
               sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();    
               bt = decoder.decodeBuffer( str );    
           } catch (IOException e) {    
               e.printStackTrace();    
           }    
           
               return new String(bt);    
           }    

}
View Code
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MyEmailUtil {
    /**
     * 邮件发送工具类
     */
        /**
         * 邮件发送的方法
         * 
         * @param to 收件人
         * @param subject 主题
         * @param content 内容
         * @param smtp 协议
         * @param host 发送服务器服务器
         * @param sendName 邮件发送人
         * @param sendPort 邮件发送人端口
         * @param userName 邮件发送人名
         * @param userPwd 邮件发送人密码
         * @return 成功或失败
         */
        public static boolean send(String to, String subject, String content, String smtp, String host,
                String sendName, String sendPort, String userName, String userPwd) {

            // 第一步:创建Session
            Properties props = new Properties();
            // 指定邮件的传输协议,smtp(Simple Mail Transfer Protocol 简单的邮件传输协议)
            props.put("mail.transport.protocol", smtp);
            // 指定邮件发送服务器服务器 "smtp.qq.com"
            props.put("mail.host", host);
            // 指定邮件的发送人(您用来发送邮件的服务器,比如您的163sina等邮箱)
            props.put("mail.from", sendName);
            if (true) {
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.socketFactory.fallback", "false");
                props.put("mail.smtp.socketFactory.port", sendPort);
            }
            Session session = Session.getInstance(props);
/*            Session session = Session.getDefaultInstance(props);
*/
            // 开启调试模式
            session.setDebug(true);
            try {
                // 第二步:获取邮件发送对象
                Transport transport = session.getTransport();
                // 连接邮件服务器,链接您的163、sina邮箱,用户名(不带@163.com,登录邮箱的邮箱账号,不是邮箱地址)、密码
                transport.connect(userName, userPwd);
                Address toAddress = new  InternetAddress(to);

                // 第三步:创建邮件消息体
                MimeMessage message = new MimeMessage(session);
                //设置自定义发件人昵称
                String nick="";
                try {
                    nick=javax.mail.internet.MimeUtility.encodeText("kasher");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } 
                message.setFrom(new InternetAddress(nick+" <"+sendName+">"));
              //设置发信人
               // message.setFrom(new InternetAddress(sendName));

                // 邮件的主题
                message.setSubject(subject);
                //收件人
                message.addRecipient(Message.RecipientType.TO, toAddress); 
                /*//抄送人
                Address ccAddress = new InternetAddress("first.lady@whitehouse.gov"); 
                message.addRecipient(Message.RecipientType.CC, ccAddress);*/
                // 邮件的内容
                message.setContent(content, "text/html;charset=utf-8");
                // 邮件发送时间
                message.setSentDate(new Date());

                // 第四步:发送邮件
                // 第一个参数:邮件的消息体
                // 第二个参数:邮件的接收人,多个接收人用逗号隔开(test1@163.com,test2@sina.com)
                transport.sendMessage(message, InternetAddress.parse(to));
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

        public static void main(String[] args) {
            // 您要发送给谁,标题,内容
            MyEmailUtil.send("373528219@qq.com", "测试发送邮件", "从QQ发送邮件到gmail,gmail自动转发到QQ", "smtp", "smtp.qq.com", "******@qq.com此处要和后面一致", "25", "*****@qq.com此处要和前面一致", "此处是邮箱加密后的密码");
        }
}
View Code

相关推荐