springboot2.x整合Email並利用AOP做一個項目異常通知功能

因爲不知aop能幹嘛,因此用aop做個小功能,再結合最近學的springboot-Email做了個系統異常自動郵件通知的功能,

感覺滿滿的成就感。

AOP不懂的可以看上一篇https://www.cnblogs.com/zgq7/p/11310142.html

springboot-Email的整合官方文檔https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/integration.html#mail

先看看這個功能的總體規劃圖:

 

 

因此需要思考的是:

1:如何捕獲異常?

總不能在每個會發生異常的地方寫 throw 或者 try-catch 語句吧?因此利用AOP進行統一捕獲並進行下一步處理。

 

2:如何將異常信息發送到開發者(用戶)郵箱?

這就需要用到javaMail技術了,而我在maven倉庫搜尋時看到了springboot-Email,因此去自發瞭解了這個開發流程。

本來這裏打算使用原生的JavaMail的,但是springboot集成了就沒用了。

原生的可參考這裏https://www.cnblogs.com/LUA123/p/5575134.html

下面開始我的設計思路的實現流程。

 

1:先提前在qq郵箱 

 

1:添加相關springboot-mail以及springboot-aop 相關依賴

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

 

2:編寫一個切面類,用於全局捕捉程序產生的異常


package com.dev.config.aop;

import com.dev.config.LocalThreadPool;
import com.dev.model.email.EmailModel;
import com.dev.utils.email.MailSendUtils;
import com.dev.utils.exception.ExceptionCodes;
import com.dev.utils.exception.ServiceException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import java.util.Arrays;

/**
* Created on 2019-07-31 9:41.
*
* @author zgq7
*/
@Aspect
@Order(2)
public class RuntimeExceptionAspectJ {
@Autowired
private MailSendUtils mailSendUtils;

@Autowired
private LocalThreadPool localThreadPool;

private final Logger log = LoggerFactory.getLogger(this.getClass());

//@Pointcut("execution(public * com.dev..*(..))")
@Pointcut("execution(public * com.dev.controller.TestController.*(..))")
private void runtimeExceptionAspect() {
}

/**
* 切面報錯
**/
@AfterThrowing(value = "runtimeExceptionAspect()", throwing = "exception")
public void afterThrowing(Throwable exception) {
Class klass = exception.getClass();
log.error("occured a [{}] , msg : [{}]", klass.getSimpleName(), ExceptionCodes.getMsgByKlass(klass));
EmailModel emailModel = new EmailModel();
emailModel.setEmailTheme("測試");
emailModel.setRecieverName("董昕傑");
emailModel.setEmailContent(exception.toString() + ":\n" + Arrays.toString(exception.getStackTrace()));
//emailModel.setRecieverEmailAddress("[email protected]");
emailModel.setRecieverEmailAddress("[email protected]");

mailSendUtils.sendEmailAsSysExceptionHtml(emailModel);
throw new ServiceException(ExceptionCodes.getCodeByKlass(klass), ExceptionCodes.getMsgByKlass(klass));
}

}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章