Search in sources :

Example 1 with ExceptionNoticeResponse

use of com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse in project ballcat by ballcat-projects.

the class AbstractNoticeGlobalExceptionHandler method run.

@Override
@SuppressWarnings("all")
public void run() {
    String key;
    TimeInterval interval = new TimeInterval();
    long threadId = Thread.currentThread().getId();
    while (true) {
        int i = 0;
        while (i < config.getMax() && interval.intervalSecond() < config.getTime()) {
            Throwable t = null;
            try {
                // 如果 i=0,即 当前未处理异常,则等待超时时间为 1 小时, 否则为 10 秒
                t = queue.poll(i == 0 ? TimeUnit.HOURS.toSeconds(1) : 10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (t != null) {
                key = t.getMessage() == null ? NULL_MESSAGE_KEY : t.getMessage();
                // i++
                if (i++ == 0) {
                    // 第一次收到数据, 重置计时
                    interval.restart();
                    messages.put(key, init(t).setKey(key).setThreadId(threadId));
                } else {
                    if (messages.containsKey(key)) {
                        messages.put(key, messages.get(key).increment());
                    } else {
                        messages.put(key, init(t).setKey(key).setThreadId(threadId));
                    }
                }
            }
        }
        // 一次处理结束
        if (messages.size() > 0) {
            // 如果需要发送的消息不为空
            messages.forEach((k, v) -> {
                try {
                    ExceptionNoticeResponse response = send(v);
                    if (!response.isSuccess()) {
                        log.error("消息通知发送失败! msg: {}", response.getErrMsg());
                    }
                } catch (Exception e) {
                    log.error("消息通知时发生异常", e);
                }
            });
            messages.clear();
        }
        interval.restart();
    }
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval) ExceptionNoticeResponse(com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse)

Example 2 with ExceptionNoticeResponse

use of com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse in project ballcat by ballcat-projects.

the class MailGlobalExceptionHandler method send.

@Override
public ExceptionNoticeResponse send(ExceptionMessage sendMessage) {
    String[] to = config.getReceiveEmails().toArray(new String[0]);
    MailSendInfo mailSendInfo = sender.sendTextMail("异常警告", sendMessage.toString(), to);
    // 邮箱发送失败会抛出异常,否则视作发送成功
    return new ExceptionNoticeResponse().setSuccess(mailSendInfo.getSuccess()).setErrMsg(mailSendInfo.getErrorMsg());
}
Also used : ExceptionNoticeResponse(com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse) MailSendInfo(com.hccake.ballcat.common.mail.model.MailSendInfo)

Aggregations

ExceptionNoticeResponse (com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse)2 TimeInterval (cn.hutool.core.date.TimeInterval)1 MailSendInfo (com.hccake.ballcat.common.mail.model.MailSendInfo)1