✨ 添加邮件通知异常信息实现类
This commit is contained in:
@@ -21,6 +21,11 @@
|
||||
<artifactId>ballcat-extend-ding-talk</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-spring-boot-starter-mail</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!--mybatis plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
||||
@@ -5,6 +5,8 @@ import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -30,4 +32,8 @@ public class ExceptionHandleConfig {
|
||||
* 堆栈转string 的长度
|
||||
*/
|
||||
private int length = 3000;
|
||||
/**
|
||||
* 接收异常通知邮件的邮箱
|
||||
*/
|
||||
private Set<String> receiveEmails = new HashSet<>();
|
||||
}
|
||||
|
||||
@@ -3,12 +3,16 @@ package com.hccake.ballcat.common.conf.exception;
|
||||
import com.hccake.ballcat.common.conf.config.ExceptionHandleConfig;
|
||||
import com.hccake.ballcat.common.conf.exception.handler.DefaultGlobalExceptionHandler;
|
||||
import com.hccake.ballcat.common.conf.exception.handler.DingTalkGlobalExceptionHandler;
|
||||
import com.hccake.ballcat.common.conf.exception.handler.MailGlobalExceptionHandler;
|
||||
import com.hccake.ballcat.common.core.exception.handler.GlobalExceptionHandler;
|
||||
import com.hccake.ballcat.common.mail.service.MailSender;
|
||||
import com.hccake.extend.ding.talk.DingTalkSender;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
@@ -45,8 +49,21 @@ public class ExceptionHandleAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(GlobalExceptionHandler.class)
|
||||
@ConditionalOnProperty(prefix = "ballcat.exception", name = "type", havingValue = "DING_TALK")
|
||||
public GlobalExceptionHandler dingTalkGlobalExceptionHandler(ExceptionHandleConfig exceptionHandleConfig, DingTalkSender sender) {
|
||||
return new DingTalkGlobalExceptionHandler(exceptionHandleConfig, sender, applicationName);
|
||||
public GlobalExceptionHandler dingTalkGlobalExceptionHandler(ExceptionHandleConfig exceptionHandleConfig, ApplicationContext context) {
|
||||
return new DingTalkGlobalExceptionHandler(exceptionHandleConfig, context.getBean(DingTalkSender.class), applicationName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮件消息通知的日志处理器
|
||||
*
|
||||
* @author lingting 2020-06-12 00:32:40
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnClass(MailSender.class)
|
||||
@ConditionalOnMissingBean(GlobalExceptionHandler.class)
|
||||
@ConditionalOnProperty(prefix = "ballcat.exception", name = "type", havingValue = "MAIL")
|
||||
public GlobalExceptionHandler mailGlobalExceptionHandler(ExceptionHandleConfig exceptionHandleConfig, ApplicationContext context) {
|
||||
return new MailGlobalExceptionHandler(exceptionHandleConfig, context.getBean(MailSender.class), applicationName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractNoticeGlobalExceptionHandler extends Thread implements GlobalExceptionHandler {
|
||||
private final ExceptionHandleConfig config;
|
||||
protected final ExceptionHandleConfig config;
|
||||
/**
|
||||
* 通知消息存放 e.message 堆栈信息
|
||||
*/
|
||||
@@ -61,10 +61,12 @@ public abstract class AbstractNoticeGlobalExceptionHandler extends Thread implem
|
||||
public void handle(Throwable e) {
|
||||
synchronized (lock) {
|
||||
number++;
|
||||
ExceptionMessage message = messages.getOrDefault(
|
||||
e.getMessage(),
|
||||
new ExceptionMessage().setNumber(0).setMac(mac).setApplicationName(applicationName)
|
||||
);
|
||||
ExceptionMessage message = messages.get(e.getMessage());
|
||||
|
||||
if (message == null) {
|
||||
message = new ExceptionMessage().setNumber(0).setMac(mac).setApplicationName(applicationName);
|
||||
}
|
||||
|
||||
message.setNumber(message.getNumber() + 1)
|
||||
.setStack(ExceptionUtil.stacktraceToString(e, config.getLength()).replaceAll("\\r", ""))
|
||||
.setTime(DateUtil.now())
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.hccake.ballcat.common.conf.exception.handler;
|
||||
|
||||
import com.hccake.ballcat.common.conf.config.ExceptionHandleConfig;
|
||||
import com.hccake.ballcat.common.conf.exception.domain.ExceptionMessage;
|
||||
import com.hccake.ballcat.common.conf.exception.domain.ExceptionNoticeResponse;
|
||||
import com.hccake.ballcat.common.mail.dto.MailDTO;
|
||||
import com.hccake.ballcat.common.mail.service.MailSender;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 钉钉消息通知
|
||||
*
|
||||
* @author lingting 2020/6/12 0:25
|
||||
*/
|
||||
@Slf4j
|
||||
public class MailGlobalExceptionHandler extends AbstractNoticeGlobalExceptionHandler {
|
||||
private final MailSender sender;
|
||||
|
||||
public MailGlobalExceptionHandler(ExceptionHandleConfig config, MailSender sender, String applicationName) {
|
||||
super(config, applicationName);
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExceptionNoticeResponse send(ExceptionMessage sendMessage) {
|
||||
MailDTO mail = new MailDTO();
|
||||
mail.setTo(String.join(MailDTO.MAIL_DELIMITER, config.getReceiveEmails()));
|
||||
mail.setSubject("异常警告");
|
||||
mail.setContent(sendMessage.toString());
|
||||
sender.sendMail(mail);
|
||||
// 邮箱发送失败会抛出异常,否则视作发送成功
|
||||
return new ExceptionNoticeResponse().setSuccess(mail.getSuccess()).setErrMsg(mail.getErrorMsg());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user