MqAspect.java 2.98 KB
Newer Older
yanzg's avatar
yanzg committed
1 2
package com.yanzuoguang.cloud.aop;

yanzg's avatar
yanzg committed
3
import com.yanzuoguang.mq.plan.YzgMqConsumer;
yanzg's avatar
yanzg committed
4
import com.yanzuoguang.util.exception.ExceptionHelper;
yanzg's avatar
yanzg committed
5
import com.yanzuoguang.util.vo.LogVo;
yanzg's avatar
yanzg committed
6 7 8 9
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
yanzg's avatar
yanzg committed
10
import org.aspectj.lang.reflect.MethodSignature;
yanzg's avatar
yanzg committed
11 12
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
yanzg's avatar
yanzg committed
13 14
import org.springframework.stereotype.Component;

yanzg's avatar
yanzg committed
15 16
import java.lang.reflect.Method;

yanzg's avatar
yanzg committed
17 18 19 20 21 22 23 24 25 26
/**
 * LogsAspect(接口请求日志切面)
 *
 * @author: Kang
 * @time: 2018年04月25日 11:43
 */
@Aspect
@Component
public class MqAspect extends BaseRequestAspect {

yanzg's avatar
yanzg committed
27
    private static final String TAG = MqAspect.class.getSimpleName();
yanzg's avatar
yanzg committed
28 29 30 31 32

    /**
     * exec aop point aspect
     */
    @Pointcut("execution(* *..mq..*Consumer.*(..))")
yanzg's avatar
yanzg committed
33
    public void mqAspect() {
yanzg's avatar
yanzg committed
34 35 36 37 38 39 40 41
    }

    /**
     * 执行环形切面
     *
     * @param joinPoint
     * @return
     */
yanzg's avatar
yanzg committed
42
    @Around(value = "mqAspect()")
yanzg's avatar
yanzg committed
43
    public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
44 45
        boolean clear = isRabbit(joinPoint);
        if (clear) {
yanzg's avatar
yanzg committed
46
            clear = clear && requestLogInit();
yanzg's avatar
yanzg committed
47
        }
yanzg's avatar
yanzg committed
48 49 50 51 52 53
        Class declaringType = joinPoint.getSignature().getDeclaringType();
        String url = getMethodUrl(joinPoint);
        String requestBody = getRequestBody(joinPoint);
        long start = requestLog(declaringType, TAG, url, requestBody, clear);
        LogVo log = startLog(TAG, url, requestBody);

yanzg's avatar
yanzg committed
54 55
        Object result = null;
        Exception ex = null;
yanzg's avatar
yanzg committed
56
        try {
yanzg's avatar
yanzg committed
57 58
            result = executeMethod(joinPoint);
            return result;
yanzg's avatar
yanzg committed
59 60
        } catch (Exception e) {
            // 消费消息出错
yanzg's avatar
yanzg committed
61 62
            result = ExceptionHelper.getError(e);
            ex = e;
yanzg's avatar
yanzg committed
63 64
            throw e;
        } finally {
yanzg's avatar
yanzg committed
65
            responseLog(declaringType, TAG, url, clear, start, getRequestBody(joinPoint), result, ex, log);
yanzg's avatar
yanzg committed
66 67 68
        }
    }

yanzg's avatar
yanzg committed
69
    private boolean isRabbit(ProceedingJoinPoint joinPoint) {
yanzg's avatar
yanzg committed
70 71 72 73 74 75 76 77 78 79 80 81
        boolean ret = false;
        if (!(joinPoint.getSignature() instanceof MethodSignature)) {
            return false;
        }
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        if (YzgMqConsumer.class.equals(targetMethod.getDeclaringClass())) {
            ret = false;
        } else {
            RabbitListener annotation = targetMethod.getAnnotation(RabbitListener.class);
            if (annotation != null) {
                ret = true;
82
            }
yanzg's avatar
yanzg committed
83 84 85 86
            if (!ret) {
                Message message = getMessage(joinPoint.getArgs());
                if (message != null) {
                    ret = true;
yanzg's avatar
yanzg committed
87 88 89
                }
            }
        }
yanzg's avatar
yanzg committed
90
        return ret;
yanzg's avatar
yanzg committed
91 92
    }

yanzg's avatar
yanzg committed
93
    private Object executeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
yanzg's avatar
yanzg committed
94 95 96
        return joinPoint.proceed();
    }
}