1,pom 依赖
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-aop
- </artifactId>
- </dependency>
2, 切点定义
- /**
- * 利用切面记录线程调用监控中心记录
- *
- */
- // 将 bean 注入 spring 容器
- @Component
- // 开启 aop 注解
- @Aspect
- public class ThreadCallRecoedAop {
- public Logger logger = LoggerFactory
- .getLogger(ThreadCallRecoedAop.class);// 输出日志
- @Autowired
- private RecordLogAsync recordLogAsync;
- @Pointcut("execution(* com.sigmatrix.thread.controller.ReceiveThreadStateController.receive(..))")// 切入点描
- public void controllerLog(){
- }
- /**
- * 记录线程调用监控中心日志
- * 在方法执行之前进行切面切入
- */
- @Before("controllerLog()")
- public void recordLog(JoinPoint pjp){
- Object[] args;
- try{
- logger.info("------------- 异步记录日志开始");
- // 获取参数
- args = pjp.getArgs();
- ThreadStateRecord threadStateRecord=(ThreadStateRecord) args[0];
- // 进行异步日志记录, 防止阻塞
- recordLogAsync.insertThreadCallLog(threadStateRecord);
- logger.info("------------- 异步记录日志成功");
- }catch(Throwable te){
- logger.error("异步记录日志异常信息为:",te);
- }
- }
- }
- /**
- * 异步记录日志入库
- *
- */
- @Component
- public class RecordLogAsync {
- @Autowired
- private ThreadCallRecordService threadCallRecordService;
- /**
- * 记录线程调用监控中心日志
- *
- */
- @Async
- public void insertThreadCallLog(ThreadStateRecord threadStateRecord) throws Throwable{
- ThreadCallRecord threadCallRecord =new ThreadCallRecord();
- // 记录时间
- threadCallRecord.setThreadCallTime(new Date());
- // 线程名字
- threadCallRecord.setThreadName(threadStateRecord.getThreadName());
- // 调用监控中心线程配置信息
- threadCallRecord.setCallMeaaage(threadStateRecord.getThreadCallMessage());
- // 线程状态
- threadCallRecord.setThreadState(threadStateRecord.getThreadMessage());
- threadCallRecordService.insertSelective(threadCallRecord);
- threadCallRecord =null;
- }
- }
3, 开启异步 Async 注解使用
来源: http://www.bubuko.com/infodetail-3034373.html