基于AOP实现的接口异常通知工具

背景

如何在接口或者方法出现异常时及时处理,并通知相应的人员。本文介绍的工具将会为你解决这个问题。

工具介绍

项目地址:https://github.com/zglgithubx/apinotice-spring-boot-starter

使用的技术:Spring AOP+自定义注解+CompletableFuture

核心源码:

@Around("@annotation(notice)")
public Object around(ProceedingJoinPoint pj, Notice notice) throws Throwable {
	long start = System.currentTimeMillis();
	Object proceed ;
	try {
		proceed= pj.proceed();
	} catch (RuntimeException e){
		//异步处理异常
		CompletableFuture.supplyAsync(()->{
				return getMessage(pj,e);
		},executor).whenComplete((res,throwable)->{
			if(!res.isEmpty()){
				concatError(start,res, pj);
			}
		}).exceptionally(throwable -> {
				System.err.println("【异步通知出现异常】:"+throwable);
				return throwable.getMessage();
		});
		throw e;
	}
	return proceed;
}

如何使用

引入依赖
<dependency>
    <groupId>io.github.zglgithubx</groupId>
    <artifactId>apinotice-spring-boot-starter</artifactId>
    <version>0.0.2-RELEASE</version>
</dependency>
application.yml配置
spring:
    mail:
        host: 邮箱服务器 //smtp.163.com
        username: 账号
        password: 密码(第三方应用密码,非平台的登录密码)
        default-encoding: UTF-8
        port: 465
        properties:
          mail:
            smtp:
              auth: true
              starttls:
                enable: true
                required: true
              socketFactory:
                class: javax.net.ssl.SSLSocketFactory
                port: 465
                fallback: false
abnormal:
    sender: 发件人,默认为:API助手
    # 线程池配置
    thread-pool:
        # 核心线程数
        core-pool-size: 10
        # 最大线程数
        max-pool-size: 15
        # 工作队列容量
        queue-capacity: 500
        # 线程池维护线程所允许的空闲时间
        keep-alive-seconds: 300
        # 拒绝策略
        rejected-execution-handler: CallerRunsPolicy
使用注解

此功能的核心是使用注解@Notice,包含两个属性,author:作者,email:接收提醒的邮箱地址。

该注解也可以加到其他业务方法上,只需要在方法上添加@Notice注解即可。

@RestController
public class TestController {
   @GetMapping("/hello")
   @Notice(author = "张三",email = "xxx@gmail.com")
   public void test(){
      throw new RuntimeException();
   }
}

效果

image-20230301170457443

最后

有想法的小伙伴,可以私聊我,一起维护和改进这个工具。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值