1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.mq.service.MessageLogService;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.vo.LogVo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
/**
* LogsAspect(接口请求日志切面)
*
* @author: Kang
* @time: 2018年04月25日 11:43
*/
@Aspect
@Component
public class AspectMq {
private static final String TAG = AspectMq.class.getSimpleName();
private final MessageLogService logService;
private final AspectLogUrl aspectLogUrl;
private final AspectLogBody aspectLogBody;
private final AspectLogStart aspectLogStart;
private final AspectLogResult aspectLogResult;
public AspectMq(MessageLogService logService, AspectLogUrl aspectLogUrl, AspectLogBody aspectLogBody, AspectLogStart aspectLogStart, AspectLogResult aspectLogResult) {
this.logService = logService;
this.aspectLogUrl = aspectLogUrl;
this.aspectLogBody = aspectLogBody;
this.aspectLogStart = aspectLogStart;
this.aspectLogResult = aspectLogResult;
}
/**
* exec aop point aspect
*/
@Pointcut("execution(* *..mq..*Consumer.*(..))")
// @Pointcut("execution(* org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(..))")
public void mqAspect() {
}
/**
* 执行环形切面
*
* @param joinPoint 方法提
* @return 执行回掉
*/
@Around(value = "mqAspect()")
public Object requestMqAround(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> declaringType = joinPoint.getSignature().getDeclaringType();
Message message = aspectLogUrl.getMessage(joinPoint);
if (message == null) {
return executeMethod(joinPoint);
}
logService.logCurrent(message);
String url = aspectLogUrl.getMessageUrl(joinPoint, message);
String requestBody = aspectLogBody.getRequestBody(message);
LogVo log = new LogVo();
boolean clear = true;
aspectLogStart.requestLogInit();
aspectLogStart.requestLog(declaringType, TAG, url, requestBody, log, clear);
long start = System.currentTimeMillis();
Object result = null;
Exception ex = null;
try {
result = executeMethod(joinPoint);
return result;
} catch (Exception e) {
// 消费消息出错
result = ExceptionHelper.getError(e);
ex = e;
throw e;
} finally {
logService.logCurrentRemove();
aspectLogResult.responseLog(declaringType, TAG, url, clear, start, requestBody, result, ex, log);
}
}
/**
* 执行函数
*
* @param joinPoint
* @return
* @throws Throwable
*/
private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
}